avatar
c*t
1
Write a program to determine whether n/2 distintinctve pairs can be formed
from given n integers where n is even and each pair's sum is divisible by
given k. Numbers cannot be repeated in the pairs, that means only you can
form total n/2 pairs.
我想到的方法
find num%k for each number in the array and sum them. If it is divisible by
K then we have n/2 pairs
被鄙视了。为什么?
avatar
h*9
2

by
充分和必要条件的区别。试试 { 3, 4, 7, 10 } & k = 3.

【在 c********t 的大作中提到】
: Write a program to determine whether n/2 distintinctve pairs can be formed
: from given n integers where n is even and each pair's sum is divisible by
: given k. Numbers cannot be repeated in the pairs, that means only you can
: form total n/2 pairs.
: 我想到的方法
: find num%k for each number in the array and sum them. If it is divisible by
: K then we have n/2 pairs
: 被鄙视了。为什么?

avatar
h*9
3
public class Divisible {
public static void main(String args[]) {
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int a2[] = { 1, 2, 3, 4, 7, 10 };
System.out.println(isDivisible(a1, 5));
System.out.println(isDivisible(a2, 3));
}

public static boolean isDivisible(int a[], int k) {
assert (a != null && a.length % 2 == 0);
assert (k != 0);

int count[] = new int[k];
for(int i = 0; i < a.length; i++) {
count[a[i] % k]++;
}
if(count[0] % 2 != 0) {
return false;
}
int i = 1;
int j = k - 1;
while(i < j) {
if(count[i++] != count[j--]) {
return false;
}
}
if(i == j && count[i] % 2 != 0) {
return false;
}
return true;
}
}
avatar
g*y
4
可以再写短一点:(更正版)
public boolean isDivisible(int[] a, int k) {
if (a==null || a.length%2==1) return false;
int[] count = new int[k];
for (int i : a) count[i%k]++;
for (int i=1; iif (count[i] != count[k-i]) return false;
}
return count[0]%2==0;
}
其他判断已经被限制,就不用测了。

【在 h**********9 的大作中提到】
: public class Divisible {
: public static void main(String args[]) {
: int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
: int a2[] = { 1, 2, 3, 4, 7, 10 };
: System.out.println(isDivisible(a1, 5));
: System.out.println(isDivisible(a2, 3));
: }
:
: public static boolean isDivisible(int a[], int k) {
: assert (a != null && a.length % 2 == 0);

avatar
C*U
5
我的想法是把这些数根据%k归类
然后从互余类里面取出数来

by

【在 c********t 的大作中提到】
: Write a program to determine whether n/2 distintinctve pairs can be formed
: from given n integers where n is even and each pair's sum is divisible by
: given k. Numbers cannot be repeated in the pairs, that means only you can
: form total n/2 pairs.
: 我想到的方法
: find num%k for each number in the array and sum them. If it is divisible by
: K then we have n/2 pairs
: 被鄙视了。为什么?

avatar
h*9
6

赞简洁,再看了一下,可以把 "return k%2==1 || count[k/2]%2==0;" 改成 "return
count[0]%2==0;"。

【在 g**********y 的大作中提到】
: 可以再写短一点:(更正版)
: public boolean isDivisible(int[] a, int k) {
: if (a==null || a.length%2==1) return false;
: int[] count = new int[k];
: for (int i : a) count[i%k]++;
: for (int i=1; i: if (count[i] != count[k-i]) return false;
: }
: return count[0]%2==0;
: }

avatar
g*y
7
赞,这个更简单

★ 发自iPhone App: ChineseWeb 7.5

【在 h**********9 的大作中提到】
:
: 赞简洁,再看了一下,可以把 "return k%2==1 || count[k/2]%2==0;" 改成 "return
: count[0]%2==0;"。

avatar
c*t
8
赞你们,不过好像最后的判断有个bug
我感觉应该是
return(k%2==1 && count[k/2] == count[k/2+1]) || (k%2==0 && count[0]%2==0);
不知能不能再简化。

【在 g**********y 的大作中提到】
: 赞,这个更简单
:
: ★ 发自iPhone App: ChineseWeb 7.5

avatar
c*t
9
试试{ 1, 2, 2, 4},5 , 出错

【在 g**********y 的大作中提到】
: 赞,这个更简单
:
: ★ 发自iPhone App: ChineseWeb 7.5

avatar
c*t
10
明白了 循环判断中间改成 i<=k/2, 最后一行用 return count[0]%2==0即可

【在 c********t 的大作中提到】
: 赞你们,不过好像最后的判断有个bug
: 我感觉应该是
: return(k%2==1 && count[k/2] == count[k/2+1]) || (k%2==0 && count[0]%2==0);
: 不知能不能再简化。

avatar
g*y
11
不需要那么多判断,不信你写unit test试试

【在 c********t 的大作中提到】
: 赞你们,不过好像最后的判断有个bug
: 我感觉应该是
: return(k%2==1 && count[k/2] == count[k/2+1]) || (k%2==0 && count[0]%2==0);
: 不知能不能再简化。

avatar
e*e
12
public static boolean isDivisible(ArrayList a, int K) {

if ( a.size() == 0 )
return true;

for ( int i = 0; i < a.size(); i++ ) {
for ( int j = i + 1; j < a.size(); j++ ) {
if ( ( a.get( i ) + a.get( j ) ) % K == 0 ) {
ArrayList c = (ArrayList)a.clone();
c.remove(a.get(i));
c.remove(a.get(j));
return isDivisible(c, K) ;
}
}
}
return false;
}
avatar
r*c
13
this is n^2?

【在 e****e 的大作中提到】
: public static boolean isDivisible(ArrayList a, int K) {
:
: if ( a.size() == 0 )
: return true;
:
: for ( int i = 0; i < a.size(); i++ ) {
: for ( int j = i + 1; j < a.size(); j++ ) {
: if ( ( a.get( i ) + a.get( j ) ) % K == 0 ) {
: ArrayList c = (ArrayList)a.clone();
: c.remove(a.get(i));

avatar
e*e
14
not sure. maybe n!.

【在 r****c 的大作中提到】
: this is n^2?
avatar
g*y
15
你是对的,我把边界写错了。但是不能这样改,应该是 i < (k+1)/2

【在 c********t 的大作中提到】
: 明白了 循环判断中间改成 i<=k/2, 最后一行用 return count[0]%2==0即可
avatar
h*n
16
the difference betwen i<=k/2, and i < (k+1)/2 is ?

【在 g**********y 的大作中提到】
: 你是对的,我把边界写错了。但是不能这样改,应该是 i < (k+1)/2
avatar
g*y
17
6

【在 h*****n 的大作中提到】
: the difference betwen i<=k/2, and i < (k+1)/2 is ?
avatar
c*t
18
虽说我的也没有错,因为哪怕改成 i你这个很明确。

【在 g**********y 的大作中提到】
: 你是对的,我把边界写错了。但是不能这样改,应该是 i < (k+1)/2
avatar
I*g
19
二分图最大匹配吧
avatar
g*y
20
是我想错了,按里面的循环,多计算一次没关系

★ 发自iPhone App: ChineseWeb 7.5

【在 c********t 的大作中提到】
: 虽说我的也没有错,因为哪怕改成 i: 你这个很明确。
avatar
h*n
21
...
brain short circuit of mine..

【在 g**********y 的大作中提到】
: 6
avatar
i*e
22
如果可以用O(K)空间的话,counting算法的应用,可能会使逻辑看起来比较简单
int rm_k[k] = {0,...,0}
for each n in the sequence
rm_k[n%k] += 1
如果余数为0的数的个数为奇数,则至少有一个不能配对成功
if rm_k[0] % 2 == 1: return false
如果k为偶数,且余数为k/2的数的个数为奇数,,则至少有一个不能配对成功
if k % 2 == 0 && rm_k[k/2] % 2 == 1: return false
余数为i的数,必须与余数为k-i的数配对
for i from 1 to k/2
if rm_k[i] != rm_k[k-i]: return false
return true
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。