我想到了两种 还有别的嘛- int a,b,c,sum ;
- StringBuilder sb = new StringBuilder();
- for (int i =100;i<1000;i++){
- sb.append(i);
- a = sb.charAt(0)-48;
- b = sb.charAt(1)-48;
- c = sb.charAt(2)-48;
- sum = a*a*a+b*b*b+c*c*c;
- if(sum == i){
- System.out.println(i);
- }
- sb.delete(0,sb.length());
- }
复制代码
第二种是用求余法
- int a,b,c,count=0;
- for(int i =100;i<1000;i++){
- a = i % 10;
- b = i/10 %10;
- c = i/100 %10;
- if (i == a*a*a+b*b*b+c*c*c){
- System.out.println(i);
- count++;
- }
- }
- System.out.println("count is "+count);
复制代码
|
|