PKU ACM- 1016 题 java Numbers That Count

哭了~这道题在提示里面说是送分题,我却老是被提示Wrong Answer可是在eclipse里面明明运行正确的啊~想了好长时间了哎,先把代码贴出来吧,大家帮我看看问题到底出在哪,实在没办法只能去问老师了


更新:已经搞定!!原因是多迭代了一次,以至于本来应该15次结束,弄成了16次结束。比如说“0000000”这个例子,答案应该是can not be classified after 15 iterations但是在原来我的程序里面变成了loop of length 2,原因是第16次迭代的时候刚刚能测出循环。
import java.util.ArrayList; import java.util.Scanner; public class Main{ public static void main(String[] args)throws Exception{ Scanner cin=new Scanner(System.in); while(cin.hasNext()){ ArrayList<String> al=new ArrayList<String>(); String s=cin.next(); if(s.equals("-1"))continue; int j=0; doTest(s,s,j,al); } } public static void doTest(String source,String str,int j,ArrayList<String> al){ int[] c={0,0,0,0,0,0,0,0,0,0}; for(int i=0;i<str.length();i++){ if(str.charAt(i)==48)c[0]++; if(str.charAt(i)==49)c[1]++; if(str.charAt(i)==50)c[2]++; if(str.charAt(i)==51)c[3]++; if(str.charAt(i)==52)c[4]++; if(str.charAt(i)==53)c[5]++; if(str.charAt(i)==54)c[6]++; if(str.charAt(i)==55)c[7]++; if(str.charAt(i)==56)c[8]++; if(str.charAt(i)==57)c[9]++; } String Sb=new String(); for(int i=0;i<=9;i++){ if(c[i]!=0)Sb=Sb+c[i]+i; } //System.out.println(Sb); if(Sb.equals(str)){ if(j==0){ System.out.println(source+" is self-inventorying"); }else{ System.out.println(source+" is self-inventorying after "+j+" steps"); } }else{ int k=0; for(k=0;k<j;k++){ if(Sb.equals(al.get(k))){ System.out.println(source+" enters an inventory loop of length "+(j-k)); return; } } al.add(Sb); str=Sb; if(j==14)System.out.println(source+" can not be classified after 15 iterations"); j++; if(j<15){ doTest(source,str,j,al); } } } }
具体题目是这样的,给一个号码串,然后对里面不同的数字记录个数比如31123314其中有3个1,1个2,3个3,1个4然后去掉“,”和“个”得到的是31123314,这样和原来那个就一样了,这是第一次种情况,第二种是用相同的方式迭代但是第一次得不出和原来的一样的结果,而是需要进行J次迭代后才会结果保持恒定,第三种是到后面结果变成了一个以k为大小的循环。第四种就是超出15次迭代还没出现以上三种情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值