clam_clam的专栏

才疏学浅,欢迎各位大侠指点错误。感激不尽......

迅雷编程题:编程:求一个数除2余1 除3余2 除4余3 除5余4 除6余5 除7余0

package com;

public class Test {
	public static void main(String []args){
		  System.out.println(getSteps1());
		  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		  System.out.println(Test.getSteps());
		  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	}
   public static int getSteps(){//利用最小公倍数,来减少遍历次数。
	    int i=1;
		int step=2;
		boolean maxStep=false;
		while(true){
			System.out.print(" "+i+" ");
			if(!maxStep){
				if(i%2==1){step=2;}else{i+=step;continue;}
				if(i%3==2){step=6;}else{i+=step;continue;}
				if(i%4==3){step=12;}else{i+=step;continue;}
				if(i%5==4){step=60; maxStep=true;}else{i+=step;continue;}
				if(i%6==5){step=60; maxStep=true;}else{i+=step;continue;}
				if(i%7==0){ return i;}else{i+=step;continue;}
			}else{ //以后每次加60
				if(i%7==0){return i;}else{i+=step;}//else{i+=step;continue;}加了一个continue 编译器出错。
				if(i%6==5){}else{i+=step;continue;}
				if(i%5==4){}else{i+=step;continue;}
				if(i%4==3){}else{i+=step;continue;}
				if(i%3==2){}else{i+=step;continue;}
				if(i%2==1){}else{i+=step;continue;}
			}
		}
   }
   
   public static int getSteps1(){
	   int i=1;
		while(true){
				if(i%2==1){}else{i++;continue;}
				if(i%3==2){}else{i++;continue;}
				if(i%4==3){}else{i++;continue;}
				if(i%5==4){}else{i++;continue;}
				if(i%6==5){}else{i++;continue;}
				if(i%7==0){return i;}else{i++;continue;}
		}
   }
}


运行结果为:

119
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 1  3  5  11  23  35  47  59  119 119
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

看到当我采用最小公倍数办法只需要循环8次就可以找到数119。以后加60的都符合题目要求。
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭