寻找符合条件的整数

任意给定一个正整数N,求一个最小的正整数M(M >1),使得N * M 的十进制表示形式里只含有1 和0,例如:

1 * 1 = 1 2 * 5 = 10
3 * 37 = 111 4 * 25 = 100
5 * 2 = 10 6 * 185 = 1,110
7 * 143 = 1,001 8 * 125 = 1,000
9 * 12,345,679 = 111,111,111


package 已知N求M结果只有0和1;


/**
* @author kerry
* Nov 11, 2009
*/
/**
* 链表节点类,注意无元素以及仅有一个元素的情况
*/
class Node
{
public int v;
public int r;
public Node next;
public Node(int v,int r)
{
this.v=v;
this.r=r;
next=null;

}


}
public class LinkedQueue {

/**
* @param args
*/
//队首指针
Node front;
//队尾指针
Node rear;

public LinkedQueue()
{
this.front=null;
this.rear=null;

}
//插入元素到队尾
public void enqueue(int v,int r)
{
Node node=new Node(v,r);
/**
* 队列不为空
*/
if(front!=null&&rear!=null)
{
rear.next=node;
//重置队尾指针
rear=rear.next;
System.out.println("enqueue is ok!!!");
}

/**
* 队列为空
*/
else{
rear=node;
front=rear;
System.out.println("enqueue is ok too!!!");
}

}
public Node dequeue(){

if(front==null)
System.out.println("queue is empty!!!");

/**
* 涉及两个指针,必须考虑单个对象时的情况
*/
//队列只剩一个节点
Node tempNode=null;
if(front==rear&&rear!=null)
{
//保存删除节点
tempNode=front;
front=null;
rear=null;

}
else {
tempNode=front;
front=front.next;
}
return tempNode;


}
public void display(){

Node current=front;
if (current==null) {
System.out.println("队列为空");
}
//kerry,注意此条件,需要研究!!!
while (current!=null&&current!=rear.next) {

System.out.println(current.v);
//kerry,不要忘记移动指针!!!
current=current.next;

}

}
public boolean isEmpty(){

if (front==null) {
return true;
}
return false;


}


public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedQueue linkedQueue=new LinkedQueue();
linkedQueue.enqueue(1,1);
linkedQueue.enqueue(2,2);
linkedQueue.enqueue(3,3);
linkedQueue.dequeue();
linkedQueue.display();

}

}





package 已知N求M结果只有0和1;

import java.util.Queue;
import java.util.Vector;
import com.datastructure.queue.*;

/**
*
* @author kerry
* Sep 23, 2010
*/
public class GetMinInt {
//保存余数
private static Vector<Integer> r=new Vector<Integer>();
//给定数
private static int N=17;
//初始化队列
LinkedQueue queue=new LinkedQueue();

//判断余数是否记录过,没有的话,记录下来
boolean isRecord(Vector<Integer> r,int i){
r.add(1);
boolean flag=false;
int j;
if(!r.isEmpty())
{
for (j = 0; j < r.size(); j++) {
if (i==r.get(j)) {
System.out.println("recorded!!!");
flag=true;
}
}
}
//未找到,保存余数
if(!flag)
{
r.add(r.lastElement()+1, i);
System.out.println("hava saved remiander!");
}
return flag;



}
public void GetMinInt(){


queue.enqueue(1,1);
while (!queue.isEmpty()) {
Node tempNode=queue.dequeue();
if ((tempNode.r%N)==0) {
System.out.println("N="+N+"M="+tempNode.v/N);

}
//记录余数,没有则加入
isRecord(r,tempNode.r%N);
//判断余数是否记录过,如果记录过且在同一层,则其剪掉节点,否则左右子树节点入队列
if (!isRecord(r,tempNode.r*10%N)) {
queue.enqueue(tempNode.v*10,tempNode.r*10%N);
System.out.println("add left node");

}
if (!isRecord(r,(tempNode.r*10+1)%N)) {
queue.enqueue(tempNode.v*10+1,(tempNode.r*10+1)%N);
System.out.println("add right node");

}



}
}
public static void main(String[] args) {

GetMinInt getMinInt=new GetMinInt();
getMinInt.GetMinInt();





}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值