程序运行总时间大于3s?

what易 2014-02-10 03:02:44
一种报数游戏是从1开始连续报数,如果报到7的倍数(7,14,21,28……)或者包含数字7的数(7,17,27,37……)就用拍手代替这个数而不能报出。假设你连续听到m声拍手,问造成你听到m声拍手的第一下拍手所代表的数是几?例如,你听到了两次连续的拍手,最小的可能这两次拍手是27和28,因此输出27。 输入m, 输出这m次连续的拍手第一下所代表的最小可能的数。

我的现实方式是:


public class Test
{
public static int number(int m)
{
int one=1;
boolean b=true;
int chenggong=0;
while(b){


chenggong=0;
for (int i = 0; i < m; i++) {
if((one+i)%7==0||(one+i-7)%10==0){

chenggong++;
}

}
if(chenggong==m){

b=false;
}else{
b=true;
}


one++;
}

return one-1;
}
//start 提示:自动阅卷起始唯一标识,请勿删除或增加。
public static void main(String args[])
{



long startTime=System.currentTimeMillis(); //获取开始时间

System.out.println(number(2));
long j=0;
while(j<10000.0){
System.out.println(j);
j++;
}
long endTime=System.currentTimeMillis(); //获取结束时间

System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
}
//end //提示:自动阅卷结束唯一标识,请勿删除或增加。
}
...全文
287 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
what易 2014-02-14
  • 打赏
  • 举报
回复
额 ,我也看错 题目 了 ,我说 怎么可能会 出现 2次以上的连续拍
kardelpeng 2014-02-13
  • 打赏
  • 举报
回复
引用 5 楼 u010111184 的回复:
[quote=引用 4 楼 kardelpeng 的回复:]
static void intPaiShou(int count){
		int i = 1;
		//连续次数
		int lCount = 0;
		int[] intArr = new int[count];
		while(true){
			System.out.println(i);
			boolean isContinue = false;
			for(int x = 0; x < count; x++){
				//如果报到7的倍数(7,14,21,28……)或者包含数字7的数(7,17,27,37……)
				if((i - x) != 0 && (i - x) % 7 == 0 || (i - x) % 10 == 7){
					isContinue = true;
					intArr[x] = (i - x);
				}else{
					isContinue = false;
					break;
				}
			}
			if(isContinue){
				for(int j : intArr){
					System.out.println(j);
				}
				break;
			}
			i++;
		}
	}
算法有问题。 (i - x) % 10 == 7只能保证末尾是7~[/quote] 嗯,理解错误。

static void intPaiShou(int count,int n){
		int i = 1;
		int[] intArr = new int[count];
		while(true){
			System.out.println(i);
			boolean isContinue = false;
			for(int x = 0; x < count; x++){
				//如果报到7的倍数(7,14,21,28……)或者包含数字7的数(7,17,27,37……)
				if((i - x) != 0 && (i - x) % 7 == 0 || (i - x) % 10 == 7 || include7(i - x)){
					isContinue = true;
					intArr[x] = (i - x);
				}else{
					isContinue = false;
					break;
				}
			}
			if(isContinue){
				for(int j : intArr){
					System.out.println("连续拍" + count + "手的num:" + j);
				}
				break;
			}
			i++;
		}
	}
 	
 	
 	 static boolean include7(long i){
         String str=String.valueOf(i);
         return str != null && str.trim().indexOf("7") != -1;
 	 }
这样修改即可
逍遥jc 2014-02-12
  • 打赏
  • 举报
回复
被7乘除当作整数处理。 包含7当作字符串处理。 这样应该比较靠谱。
逍遥jc 2014-02-12
  • 打赏
  • 举报
回复
引用 4 楼 kardelpeng 的回复:
static void intPaiShou(int count){
		int i = 1;
		//连续次数
		int lCount = 0;
		int[] intArr = new int[count];
		while(true){
			System.out.println(i);
			boolean isContinue = false;
			for(int x = 0; x < count; x++){
				//如果报到7的倍数(7,14,21,28……)或者包含数字7的数(7,17,27,37……)
				if((i - x) != 0 && (i - x) % 7 == 0 || (i - x) % 10 == 7){
					isContinue = true;
					intArr[x] = (i - x);
				}else{
					isContinue = false;
					break;
				}
			}
			if(isContinue){
				for(int j : intArr){
					System.out.println(j);
				}
				break;
			}
			i++;
		}
	}
算法有问题。 (i - x) % 10 == 7只能保证末尾是7~
kardelpeng 2014-02-12
  • 打赏
  • 举报
回复
static void intPaiShou(int count){
		int i = 1;
		//连续次数
		int lCount = 0;
		int[] intArr = new int[count];
		while(true){
			System.out.println(i);
			boolean isContinue = false;
			for(int x = 0; x < count; x++){
				//如果报到7的倍数(7,14,21,28……)或者包含数字7的数(7,17,27,37……)
				if((i - x) != 0 && (i - x) % 7 == 0 || (i - x) % 10 == 7){
					isContinue = true;
					intArr[x] = (i - x);
				}else{
					isContinue = false;
					break;
				}
			}
			if(isContinue){
				for(int j : intArr){
					System.out.println(j);
				}
				break;
			}
			i++;
		}
	}
sca4441479 2014-02-11
  • 打赏
  • 举报
回复
我的怎么不行了?
what易 2014-02-10
  • 打赏
  • 举报
回复
恩,测出来了 我的 算法是有问题,你的也不行哦
sca4441479 2014-02-10
  • 打赏
  • 举报
回复
参考下我的,你的我把次数改为20就卡死了,估计你这个if((one+i)%7==0||(one+i-7)%10==0) 算法有问题

static void paishou(int m){//输入连续m次拍手次数
    	boolean b=false;
    	for(long i=1;i<999999999;i++){
    		
    		if(i%7==0 || include7(i)){
    			int k=0;
    			for(long j=i;j-i<=m;j++){
    				if(j%7==0 || include7(j)){
    					k++;
    					if(k==m){
    						System.out.println("连续"+m+"次拍手,第一次拍手的数为:"+(j-k));
    						b=true;
    						break;
    					}
    				}else break;
    			}
    		}else continue;
    		if(b) break;
    	}
    }
    
    static boolean include7(long i){
    	String str=String.valueOf(i);
    	if(str.indexOf("7")!=-1) return true;
    	return false;
    }

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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