1。 一人岁数的3次方是四位数,四次方是六位数,并知道此人岁数的3次方和4次方用遍了0~9十个数字。编写一程序求此人的岁数。2。对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合。由于自己菜没做出来,请高手帮我分析分析这两道题。

解决方案 »

  1.   

    方法很多种的,出题者好像考察思维护,偶写了第一题,15分钟内,不过效率不是很好!    public static int getAge() {
            int age;
            int third;
            int fourth;        for (int i = 11; true; i++) {
                if (i < 200) {
                    third = (int) Math.pow(i, 3);
                    fourth = (int) Math.pow(i, 4);
                    if (getLength(third, fourth) == 10) {
                        age = i;
                        break;
                    }            }
            }
            return age;
        }    public static int getLength(int args1, int args2) {
            String str1 = String.valueOf(args1);
            String str2 = String.valueOf(args2);
            String str = str1 + str2;
            if (str.length() != 10) {
                return -1;
            }
            int[] intarray = new int[10];
            for (int i = 0; i < str.length(); i++) {
                intarray[i] = Integer.parseInt(str.substring(i,i+1));
            }
            Arrays.sort(intarray);
            if(intarray[0]!=0 && intarray[9]!=9)
                return -1;        return 10;
        }
      

  2.   

    第二题还更简单了  for(int i=1; i<6; i++){
                for(int j=1; j<6; j++){
                    if(i==j){
                        System.out.println(j+""+j);
                    }else{
                        System.out.println(i+""+j);
                        System.out.println(j+""+i);
                    }
                }
            }
      

  3.   

    public class A {
    // http://community.csdn.net/Expert/topic/4667/4667929.xml?temp=.57922
    public static void main(String[] args) {
    String t;
    String[] s = new String[5];
    int j = s.length;
    for(int i=0; i<j; i++) {
    s[i] = new Integer(i+1).toString();
    }

    for(int i=0; i<j; i++) {
    t = s[i];
    for(int a=0; a<j; a++) {
    t += s[i];
    System.out.println(t); 
    }
    System.out.println();
    }
    }
    }
      

  4.   

    第二题还更简单了  for(int i=1; i<6; i++){
                for(int j=1; j<6; j++){
                    if(i==j){
                        System.out.println(j+""+j);
                    }else{
                        System.out.println(i+""+j);
                        System.out.println(j+""+i);
                    }
                }
            }
    ============================================================
    楼上的没看清题目,它是让你对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合,所以重复的数字不应该算在里面。
    第二题应该改为:
      for(int i=1; i<6; i++){
                for(int j=1; j<6; j++){
                    if(i==j){
                        break;
                    }else{
                        System.out.println(i+""+j);
                        System.out.println(j+""+i);
                    }
                }
            }
      

  5.   

    public class B {
    public static void main(String[] args) {
    for (int i = 1; i < 6; i++) {
    int t = i;
    for(int a = 0; a<5; a++) {
    int c = a+1;
    if(c == t) {
    continue;
    }else {
    System.out.println(t*10+c);
    }
    }
    System.out.println();
    }
    }
    }
      

  6.   

    第二题
    public class Test
    {
    public static void main(String[] args)
    {
    int[][] a=new int[5][];
    for(int i=0;i<a.length;i++)
    {
    a[i]=new int[i+1];
    }
    for(int i=1;i<=a.length;i++)
    {

    for(int j=i+1;j<=a.length;j++)
    {
    System.out.print(i);
    System.out.print(j+" ");
    }
    System.out.print(" ");
    }

    for(int i=a.length;i>0;i--)
    {

    for(int j=i-1;j>0;j--)
    {
    System.out.print(i);
    System.out.print(j+" ");
    }
    System.out.print(" ");
    }
    }
    }
      

  7.   

    public class Test { public static int getDigits(String str) {
    int[] intarr = new int[10];
    for (int i = 0; i < 10; i++)
    intarr[i] = 0;
    for (int i = 0; i < str.length(); i++) {
    int j = Integer.parseInt(str.substring(i, i + 1));
    intarr[j] = 1;
    }
    int num = 0;
    for (int i = 0; i < 10; i++)
    num = num + intarr[i];
    return num;
    } private static int getAge() {
    int age;
    int third;
    int fourth;
    for (age = 1; age < 100; age++) {
    third = (int) Math.pow(age, 3);
    fourth = (int) Math.pow(age, 4);
    if (third < 1000 || third >= 10000)
    continue;
    if (fourth < 100000 || fourth >= 1000000)
    continue;
    String str = String.valueOf(third) + String.valueOf(fourth);
    if (getDigits(str) == 10)
    return age;
    }
    return 0;
    }
    }
      

  8.   

    genderwang(不再任性)
    对于你第一题的方法我有点疑问:
            if(intarray[0]!=0 && intarray[9]!=9)
                return -1;
    如果intarray数组中有重复的数字,你做的这个判断就没用了。
      

  9.   

    第二道题
    class Combine
    {
    public static void main(String[] args)
    {
        for(int i=1; i<5; i++)
        {
                    for(int j=i+1; j<6; j++)
                    {               
                 System.out.println(i+""+j);
                 System.out.println(j+""+i);                
                    }
                 }
    }

    }这个比上面几个人的方法少了一个判断,呵呵
      

  10.   

    其实第一道题可以简单点的,先根据题目条件求出年龄的范围,
    age的三次方为四位数,所以age<=21;
    age的四次方为六位数,所以age>=18;
    然后在试验这中间的数就可以了,其实里边还是有些可以取调的,比如尾数为0,1的三次方和四次方之后的尾数相同,这个肯定不能满足要求.只剩下18也19了.这样算出来不知道面试人员会怎么想?
      

  11.   

    1,2  2,1是同一种组合吧
    public class Test
    {
    public static void main(String[] args)
    {
        for(int i=1; i<6; i++)
        {
                    for(int j=i+1; j<6; j++)
                    {               
                 System.out.println(i+""+j);              
                    }
                 }
    }

    }
      

  12.   

    public class Age
    {
    public static void main(String [] args)
    {
    String str1 = null;
    String str2 = null;
    String str3 = null;
    String str4 = "0123456789";
    for(int i=10;i<50;i++)
    {
    str1 = Integer.toString(i*i*i);
    str2 = Integer.toString(i*i*i*i);
    str3 = str1+str2;
    if((str1.length() == 4) && (str2.length() ==6))
    {
    boolean flag = true;
    for(int j=0;j<10;j++)
    if(str3.indexOf(str4.charAt(j))==-1)
    flag = false;
    if(flag){
    System.out.println(">>>"+i);
    System.out.println(str3);
    }
    }
    } }
    }
      

  13.   

    to kevin4(无聊中~~) :
    呵呵,本人也是充当试者的身份来做题的,当时15分钟写的有点乱了.
    至于你说的那点,我并不会认为有重复的数字.
     1, length!=10 ->return -1;
    2, length==10 && Arrays.sort(a).
    居于四点,我认为很可能就是0-9,但还是确少根据,
    在排序后要么加入几行代码我感肯定能筛选出来,
    for(int i=0; i<10; i++){
      if(a[i]!=i)
        return -1;
    }
      

  14.   

    回复人:echomyf(ECHO) ( 四级(中级)) 信誉:100  2006-04-06 23:01:00  得分:0

    其实第一道题可以简单点的,先根据题目条件求出年龄的范围,
    age的三次方为四位数,所以age<=21;
    age的四次方为六位数,所以age>=18;
    然后在试验这中间的数就可以了,其实里边还是有些可以取调的,比如尾数为0,1的三次方和四次方之后的尾数相同,这个肯定不能满足要求.只剩下18也19了.这样算出来不知道面试人员会怎么想?==================
    一下子想到的也是这个法子
      

  15.   

    回复人:echomyf(ECHO) ( 四级(中级)) 信誉:100  2006-04-06 23:01:00  得分:0

    其实第一道题可以简单点的,先根据题目条件求出年龄的范围,
    age的三次方为四位数,所以age<=21;
    age的四次方为六位数,所以age>=18;
    然后在试验这中间的数就可以了,其实里边还是有些可以取调的,比如尾数为0,1的三次方和四次方之后的尾数相同,这个肯定不能满足要求.只剩下18也19了.这样算出来不知道面试人员会怎么想?==================支持这个方法,计算机执行效率最高  :)
      

  16.   

    回复人:echomyf(ECHO) ( 四级(中级)) 信誉:100 2006-04-06 23:01:00 得分:0

    其实第一道题可以简单点的,先根据题目条件求出年龄的范围,
    age的三次方为四位数,所以age<=21;
    age的四次方为六位数,所以age>=18;
    然后在试验这中间的数就可以了,其实里边还是有些可以取调的,比如尾数为0,1的三次方和四次方之后的尾数相同,这个肯定不能满足要求.只剩下18也19了.这样算出来不知道面试人员会怎么想?==================支持这个方法,计算机执行效率最高  :)
    真的很不错
      

  17.   

    其实第一道题可以简单点的,先根据题目条件求出年龄的范围,
    age的三次方为四位数,所以age<=21;
    age的四次方为六位数,所以age>=18;
    然后在试验这中间的数就可以了,其实里边还是有些可以取调的,比如尾数为0,1的三次方和四次方之后的尾数相同,这个肯定不能满足要求.只剩下18也19了.这样算出来不知道面试人员会怎么想?与偶的想法相同,这是面试题,只要搞出思想就可以了,代码组织不可能当场说出来吧
    支持这个解法
      

  18.   

    public class Test2 { public static void main(String[] args) {
    int i=0;
    while(true){
    i++;
    int a=i*i*i;
    if(a<1000 || a>9999)
    continue;
    int b=i*i*i*i;
    if(b<100000 || b>999999)
    continue;
    Set s=new HashSet();
    String sa=a+"";
    String sb=b+"";
    for(int j=0;j<sa.length();j++){
    s.add((char)sa.charAt(j)+"");
    }
    for(int j=0;j<sb.length();j++){
    s.add((char)sb.charAt(j)+"");
    }
    if(s.size()==10){
    System.out.println(i+":"+a+","+b);
    break;
    }
    }
    }

    }
      

  19.   

    都很强呀~不过JAVA不适合做这种东西~呵呵~公司只是为了考逻辑吧~
      

  20.   

    第一道:
    public class Test {
      public static void main(String[] args) {
        //对1,2,3, 4, 5 这五个数任意取出两个数,列出他们的所有组合。
        int m=0;
        String[] str=new String[20];
        for(int i=1;i<=5;i++)
        {
          for(int j=1;j<=5;j++)
          {
            String a=i+""+j;
            if(a.equals(j+""+j)){}
            else
              str[m++]=a;
          }
        }
        for(int i=0;i<str.length;i++)
        {
          System.out.println(str[i]);
        }
      }
    }第二道:
    public class Test {
      static boolean check(Integer a,Integer b)
      {
        String str1=a.toString();
        String str2 = b.toString();
        String str = str1 + str2;
        for (int i = 0; i < 9; i++) {
          for (int j = i + 1; j < 10; j++) {
            if (str.charAt(i) == str.charAt(j))
              return false;
          }
        }
        return true;
      }
      public static void main(String[] args) {
        double age=0;
        for( age=10;age<22;age++)
        {
          double a = Math.pow(age, 3.0);
          double b = Math.pow(age, 4);
          int aa=(int)a;
          int bb=(int)b;
          Integer aaa=new Integer(aa);
          Integer bbb=new Integer(bb);
          if((a>=1000&&a<9999)&&(b>=100000&&b<999999))
         {
             if(Test.check(aaa,bbb))
               System.out.println(age);
          }    }
      }
    }
      

  21.   

    class test{
      public static void main(String [] args)
     {
        for(int i=10;i<=30;i++)
       {
          String temp1 = String.valueOf(i*i*i);
          String temp2 = String.valuelOf(i*i*i*i);
          String total = temp1+temp2;
          int count = 0;
          char [] flag = {'0','1','2','3','4','5','6','7','8','9'};
          for(int j=0;j<total.length;j++)
          {
             for(int k = 0;k<flag.length;k++)
             {
               if(total.charAt(j) == flag[k])
               {
                   count++;
               }
          }
          if(count == 10)
          {
             System.out.println("the number is "+i);
             break;
          }    }
     }}
      

  22.   

    第一题,方法比较笨,但是可以得出结果18
    public class FindAge {
      int age=10;
      public FindAge() {
        findAge(10);//从10岁开始搜索
        if(age!=0)
          System.out.println(age);
        else{System.out.println("未找到符合条件的结果");}  }
      public void findAge(int AGENum){
        while(true){
          String pow3=new String((int)Math.pow(AGENum,3)+"");
          String pow4=new String((int)Math.pow(AGENum,4)+"");
          if(pow3.length()==4&&
             pow4.length()==6&&
             (!pow3.substring(0,1).equals(pow3.substring(1,2)))&&
             (!pow3.substring(1,2).equals(pow3.substring(2,3)))&&
             (!pow3.substring(2,3).equals(pow3.substring(3,4)))&&
             (!pow3.substring(3,4).equals(pow4.substring(0,1)))&&
             (!pow4.substring(0,1).equals(pow4.substring(1,2)))&&
             (!pow4.substring(1,2).equals(pow4.substring(2,3)))&&
             (!pow4.substring(2,3).equals(pow4.substring(3,4)))&&
             (!pow4.substring(3,4).equals(pow4.substring(4,5)))&&
             (!pow4.substring(4,5).equals(pow4.substring(5,6)))){
            age=AGENum;
            break;      }
    if(AGENum>100){ age=0;break;}
          AGENum++;
        }
      }
      public static void main(String[] args){    new FindAge();
      }
    }
    第二题应该比较简单,
      

  23.   

    第一题:public class TestAge {

    //用来确定可能出现的年龄段
    int define(int f3,int f4,int m) {
    double f1 = 0;
    double f2 = 0;
    f1 = Math.exp((0.3333)*Math.log(f3));
    f2 = Math.exp((0.25)*Math.log(f4));
    if(m == 0) return (int)Math.max(f1,f2)-1;
    return (int)Math.min(f1,f2)+1;
    }
    // 计算传入的年龄是否符合条件
    int check(int age) {
    //初试化数组
    int[] num = {0,0,0,0,0,0,0,0,0,0};
    long f = 0;
    //将年龄做计算,使之成为一个十位数
    f = (long)(Math.exp((3)*Math.log(age))+1)
    +(long)((int)(Math.exp((4)*Math.log(age))+1))*10000;
    for(int i = 0; i < 10; i++) {
    if(num[(int)(f%10)] == 1) return -1;  //如果该数组数字已经访问过,则有重复,退出循环
    num[(int)(f%10)] = 1;  //如果没被访问过,则置为1,做标志
    f = f/10; //除10
    }
    return 1; //10个数字都访问过了,没有提前退出则说明找到正确的年龄了。
    }
    public static void main(String[] args) {
    int maxAge = 0; //用来记录该岁数可能出现的范围
    int minAge = 0;

    TestAge t = new TestAge();

    //为了减轻计算量,先计算出年龄出现的大概范围 
    minAge = t.define(999,99999,0);
    maxAge = t.define(10000,1000000,1);
    //测试每个可能的年龄
    for(int age = minAge; age <= maxAge; age++) {
    if(t.check(age) == 1) {  //如果取到则打印出来,并推出程序
    System.out.print("age: " + age);
    System.exit(0);
    }
    }
    //如果遍历了所有的可能还没有打印出来,则说明条件有问题
    System.out.print("Sorry!" );
    }
    }
      

  24.   

    我也做了第二题:
    public class A 
    {
        public static void main(String[] args) 
        {
    int a=1;
    int index=0;
    int b;
    while(a<=5)
    {
        b=1;
        while(b<6)
        {
            if(a!=b)
            {
       System.out.println(a+" "+b);
             }
    b++;
         }
         a++;
    }
        }
    }
      

  25.   

    第一道题:
    class ComAge
    {
        public int a3;
        public int a4;
        public String str;
        public void Com()
        {
    int a=10;
    labl:
    while(a<100)
    {
        int a3=a*a*a;
        int a4=a3*a;
        if(a3>1000&&a3<9999)
        {
    if(a4>100000&&a4<999999)
    {
        String str1=String.valueOf(a3);
        String str2=String.valueOf(a4);
        str=str1+str2; 
        int index=0;
        int count=0;
        while(index<10)
        {
            count=index+1;
                     while(count<10)
           {
      if(str.substring(index).compareTo(str.substring(count))==0)
     {
        a++;
        break labl;
     }

     count++;
           }
           index++;
           if(index==10)
           {
      System.out.println("该人的年龄为:"+a);
      return;
           }

       }
    }
       }
       a++;
    }
        }
    }public class Age
    {
        public static void main(String ag[])
        {
            ComAge c=new ComAge();
            c.Com();

        }
    }