刚才停电了,现在贴上我的代码:- class GuessAge
- {
- //根据他的话可以判断出:
- //年龄的三次方是4位数,且不重复,所以是>1000和<=9876
- //所以从10开始判断。
- public static void main(String[] args)
- {
- int a = 10;
- while(a*a*a <= 9876)
- {
- if( a*a*a*a>100001 )
- {
- System.out.println(a);
- guess(a);
- a = a + 1;
- }
- else
- {
- a = a + 1;
- }
- }
- }
- public static void guess(int age)
- {
- int number1 = age*age*age*age;
- int number2 = age*age*age;
- get(number1,number2);
-
- }
- public static void get(int number1,int number2)
- {
- int temp ;
- int[] arr = new int[10];
- //将它年龄的三次方和四次方中的元素封装进数组
- //这里采用取余数的方法
- for ( int i = 0; i < 6 ; i++ )
- {
- temp = number1%10;
- arr[i] = temp;
- number1 = (number1 - temp)/10;
- }
- for (int i = 6; i < 10; i++ )
- {
- temp = number2%10;
- arr[i] = temp;
- number2 = (number2 - temp)/10;
- }
-
- //判断数组中的元素是否重复。
- for ( int x = 0 ; x < 10 ; x++ )
- {
- temp = arr[x];
- for ( int y = 0 ; y != x && y < 10; y++ )
- {
- if(arr[y] == temp)
- {
- System.out.println("不是他的年龄");
- return;
- }
- }
-
- }
- System.out.println("是他的年龄");
- }
- }
复制代码 |