给定正整数N,求最小的正整数M,使N*M十进制表示中只有0和1.

seaman_xh 2009-06-05 11:13:22
给定正整数N,求最小的正整数M,使N*M十进制表示中只有0和1.

扩展问题:
1.对于任意的N,一定存在M,使得N * M的乘积的十进制表示只有0和1吗?
2.怎样找出满足题目要求的N和M,使得N * M < 2^16,且N+M最大?
...全文
605 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shibenjie 2009-06-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 oven15534 的回复:]
Java code
// 给定正整数N,求最小的正整数M,使N*M十进制表示中只有0和1.
public static int only01(int n) {
int m = 0;
while(!checkOnly01(++m*n)) ;
return m;
}

public static boolean checkOnly01(int i) {
while(i > 0) {
if(i%10 >= 2)
return false;
i /= 10;
}
return true;
}
[/Quote]
经典
MT502 2009-06-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kw244137305 的回复:]
引用 4 楼 kw244137305 的回复:
这个方法更好 效率高
Java code
public static int only01(int n) {
int[] arr = {1};
int[] tmp;
while(true) {
tmp = new int[arr.length*2];
for(int i=0; i <arr.length; i++) {
System.out.println(arr[i]);
if(arr[i] % n == 0)
return arr[i]/n;

[/Quote]
想法很妙,但是没有考虑溢出,用BigInteger就好了
kw244137305 2009-06-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kw244137305 的回复:]
这个方法更好 效率高
Java code
public static int only01(int n) {
int[] arr = {1};
int[] tmp;
while(true) {
tmp = new int[arr.length*2];
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
if(arr[i] % n == 0)
return arr[i]/n;
tmp[i*2] = arr[i]*10;
tmp[i*2+1] = tm…
[/Quote]
kw244137305 2009-06-05
  • 打赏
  • 举报
回复

public static int only01(int n) {
int[] arr = {1};
int[] tmp;
while(true) {
tmp = new int[arr.length*2];
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
if(arr[i] % n == 0)
return arr[i]/n;
tmp[i*2] = arr[i]*10;
tmp[i*2+1] = tmp[i*2] + 1;
}
arr = tmp;
}
}
oven15534 2009-06-05
  • 打赏
  • 举报
回复

// 给定正整数N,求最小的正整数M,使N*M十进制表示中只有0和1.
public static int only01(int n) {
int m = 0;
while(!checkOnly01(++m*n)) ;
return m;
}

public static boolean checkOnly01(int i) {
while(i > 0) {
if(i%10 >= 2)
return false;
i /= 10;
}
return true;
}
WuBill 2009-06-05
  • 打赏
  • 举报
回复
有点挑战性啊,先帮你顶
seaman_xh 2009-06-05
  • 打赏
  • 举报
回复
还有没有其它方法了~~~

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧