cowskin
发贴: 1
积分: 0
|
于 2006-01-15 11:23
呵呵,小弟刚刚学了这个,这个是我老师写的程序,很牛哦~
public class Suixian { public static void main(String[] args) throws java.io.IOException { byte[] buf = new byte[20]; int cmdLength = System.in.read(buf); String str = new String(buf,0,cmdLength-2); int n = Integer.parseInt(str);//这里当然是输入一个位数罗,也不一定就三位吧 int low = 1,high = 1; for (int i=1;i<n;i++) low = low*10; high = low * 10; //System.out.println(low); //System.out.println(high); for (int i=low;i<high;i++) { int sum = 0; int p = i; while (p!=0) { int r = p%10; p = p/10; int rn = 1; for (int j=1;j<=n;j++) rn = rn * r; sum = sum + rn; } if (sum==i) System.out.println; } } } 不过这只是你程序的一个化简,(你的用了一个数组) 第二个方法是这样,拿三未数举例
public class SuiXian2 { public static void main(String[] args) { for (int a=1;a<=9;a++) for (int b=0;b<=9;b++) for (int c=0;c<=9;c++) if(a*a*a+b*b*b+c*c*c==100*a+10*b+c) System.out.println(100*a+10*b+c); } } 这是我上论坛来发的第一封贴子,望指教
|