class Demo {
public static void main(String[] args){
long l = 1134567880133455L;
int count[] = new int[10];
while (l > 0) {
int i = (int)(l % 10);
count[i]++;
l = l / 10;
}
for (int i = 0; i < 10; i++) {
Systemoutprintln ("count[" + i + "] = " + count[i]);
}
}
}
如果非得用switch,改成这样
class Demo {
public static void main(String[] args){
long l = 1134567880133455L;
int count0 = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
int count7 = 0;
int count8 = 0;
int count9 = 0;
while (l > 0) {
int i = (int)(l % 10);
switch (i) {
case 0: count0++; break;
case 1: count1++; break;
case 2: count2++; break;
case 3: count3++; break;
case 4: count4++; break;
case 5: count5++; break;
case 6: count6++; break;
case 7: count7++; break;
case 8: count8++; break;
case 9: count9++; break;
}
l = l / 10;
}
Systemoutprintln ("count0 = " + count0);
Systemoutprintln ("count1 = " + count1);
Systemoutprintln ("count2 = " + count2);
Systemoutprintln ("count3 = " + count3);
Systemoutprintln ("count4 = " + count4);
Systemoutprintln ("count5 = " + count5);
Systemoutprintln ("count6 = " + count6);
Systemoutprintln ("count7 = " + count7);
Systemoutprintln ("count8 = " + count8);
Systemoutprintln ("count9 = " + count9);
}
}
思路如楼上所说~看来你是新手啊,建议先去看看一些基本的东西,不要急着上手,计算机这门学科,重要的是逻辑思维,一定要逻辑非常清楚,思考问题要有条有理。
循环其实就是重复嘛
你就这样想
但是它这个重复一定是有边界的当然,你可以设置无边界,那就死循环了,一直循环
for的时候,不是有条件吗,意思就是满足条件了,就会执行循环体内的程序,不满足了,就不执行,这个循环就结束了。简单的说一下思路,把第一个数拿出来a1,因为每次都是从他开始从他结束。其余的n-1个数排列组合一下,排列成a2',a3',an'那么两组数据就已经被决定出来了。a组里面就是(a1,a2')(a3',a4')b组里(a2',a3')就出来了。所以我感觉有(n-1)!个这样的数据。不知道对不对,嘻嘻!想得太简单了?给我加点分帮你把代码写出来吧!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)