Java的算法题整理

/** */ /**父子俩的年龄:父亲今年30岁,儿子今年6岁,问多少年后父亲的年龄是儿子年龄的2倍。*/
package  exec;

public   class  Age  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        int father = 30;
        
int son = 6;
        
for(int i=1;i<100;i++)
        
...{
            
if((father+i)==((son+i)*2))...{
                System.out.println(i 
+ " 年");
            }

        }

    }

}

 

/** */ /**编写程序,将两个各有6个整数的数组,合并成一个由小到大排列的数组,(该数组的长度为12)*/
package  exec;

import  java.util.Arrays;

public   class  Arrary  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        int a[] = ...135894 };
        
int b[] = ...247659 };
        
int c[] = new int[12];
        
for (int i = 0; i < 6; i++...{
            c[i] 
= a[i];
            c[i 
+ 6= b[i];
        }

        Arrays.sort(c);
        
for (int i = 0; i < 12; i++...{
            System.out.print(c[i] 
+ " ");
        }

    }


}

 

/** */ /** 一个球从100米高度自由落下后,反弹回原来高度的一半。按此规律,到第10次落地时,行程共有多少米?
 * 然后将反弹起多高? 
*/

package  exec;

public   class  BallFall  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        float heigher = 100;
        
float sum = 0;
        
for (int i = 1; i < 10; i++...{
            heigher 
/= 2;
            sum 
+= heigher;
            System.out.println(
"" + i + "" + "总行程" + sum + "" + "反弹"
                    
+ heigher + "");
        }


    }


}

 

/** */ /** 换硬币:把一元人民币换成5分、2分、1分的硬币,有多少种换法? */
package  exec;

public   class  ChangeCoin  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        int num = 0;

        
for (int i = 1; i <= 20; i++...{
            
for (int j = 1; j <= 50; j++...{
                
for (int k = 1; k <= 100; k++...{
                    
if (5 * i + 2 * j + 1 * k == 100...{
                        num 
+= 1;
                    }

                }

            }

        }

        System.out.print(num 
+ "种换法");
    }

}

 

/** */ /**牛的繁殖问题 :有位科学家曾出了这样一道数学题:有一头母牛,它每年年初要生一头小母牛;
 * 每头小母牛从第四个年头起,每年年初也要生一头小母牛。按此规律,若无牛死亡,第20年头上共有多少头母牛?
*/

package  exec;

public   class  CountCow  ... {
    
int[] cowNumber = new int[21];

    
public void count(int increaseCycle, int years) ...{
        cowNumber[
0= 1;
        
for (int currentYear = 1; currentYear <= years; currentYear++...{
            
if (currentYear < increaseCycle) ...{
                cowNumber[currentYear] 
= cowNumber[currentYear - 1+ 1;
            }
 else ...{
                cowNumber[currentYear] 
= cowNumber[currentYear - 1]
                        
+ cowNumber[currentYear - increaseCycle + 1];

            }

            System.out
                    .println(
"" + currentYear + "" + cowNumber[currentYear]);
        }

    }


    
public static void main(String[] args) ...{
        
int increaseCycle = 4;
        
int years = 20;

        CountCow cow 
= new CountCow();
        cow.count(increaseCycle, years);
    }

}

 

/** */ /** 打印出500之内所有能被7或9整除的数*/
package  exec;

public   class  Divide  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        for (int i = 0; i <= 500; i++...{
            
if ((i % 7 == 0| (i % 9 == 0)) ...{
                System.out.print(i 
+ " ");
            }

        }

    }


}

 

/** */ /**
 * 编写一个Java程序 使它随即产生1800到2000之间的年份,打印它是否是一个闰年,闰年是1584年以后的年份,它要能被400整除,
 
 * 要能被4整除但是不能被100整除,已知使用Math.random()方法可以产生0到1之间的随即小数. )

 
*/

package  exec;

public   class  EmergerYear  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        for (int i = 0; i < 100; i++...{
            
long year = (Math.round(Math.random() * 200+ 1800);
            
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0...{
                System.out.println(year 
+ "是闰年!");
            }
 else
                System.out.println(year 
+ "不是闰年!");
        }

    }


}

 

/** */ /**编写程序以递归的方式实现1+2+3+...+n(n=200)的计算.*/
package  exec;

public   class  Number  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        System.out.print(numbers(200));
    }

    
public static int numbers(int n)...{
        
return n<=1?1:n+numbers(n-1);
    }

}

 

/** */ /** 给定一个数,并由键盘输入若干个数,找出与预先给定的数最接近的数,
 * 并指出它是由键盘输入的第几个数。 
 
*/

package  exec;

import  java.io.BufferedReader;
import  java.io.IOException;
import  java.io.InputStreamReader;

public   class  NumberCompare  ... {
    
public static final int NUMBER = 80// 给定一个要比较的数

    
public static void main(String args[]) ...{
        System.out.print(
"Please input numbers: ");
        BufferedReader io 
= new BufferedReader(new InputStreamReader(System.in));
        
try ...{
            
// 从键盘输入若干数
            String line = io.readLine();
            
// 放入数组中
            String[] items = line.split(" ");
            
int temp = 5000, x = 0, place = 0;
            System.out.println(
"target number: 80");
            
// 和给定的数比较大小
            for (int i = 0; i < items.length; i++...{
                
if (Integer.parseInt(items[i]) > NUMBER) ...{
                    x 
= Integer.parseInt(items[i]) - NUMBER;
                }
 else ...{
                    x 
= NUMBER - Integer.parseInt(items[i]);
                }

                
if (x < temp) ...{
                    temp 
= x;
                    place 
= Integer.parseInt(items[i]);
                }

            }

            System.out.println(
"place: " + place);
            io.close();
            System.exit(
0);
        }
 catch (IOException e) ...{
            e.printStackTrace();
        }

    }

}

 

/** */ /**使用Java,long.Math类,生成10个0到99之间的随即整数,求出它们中的最小值和最大值*/
package  exec;

import  java.util.Iterator;
import  java.util.TreeSet;

public   class  RandomNumber  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        /** *//**方法1
        long max = 50;
        long min = 50;
        for(int i=0; i<10; i++){
            long number = Math.round(Math.random()*99 + 1);
            System.out.print(number + " ");
            max = Math.max(max, number);
            min = Math.min(min, number);
            if(i == 9){
                System.out.print(" Math MaxNumber=" + max + " MinNumber=" + min);
            }
        }
        
*/

        
//方法2
        TreeSet ts = new TreeSet();
        
for(int i=0; i<10; i++)...{
            
long number = Math.round(Math.random()*99 + 1);
            System.out.print(number 
+ " ");
            ts.add(number);
        }

        System.out.println();
        Iterator it 
= ts.iterator();
        
while(it.hasNext())...{
            System.out.print(it.next() 
+ " ");
            
        }

        System.out.print(
" Math MaxNumber=" + ts.last() + " MinNumber=" + ts.first());
    }

        
}

 

/** */ /**假设有一条钢材长2000米,每天截取其中的一半,编写一程序求出多少天后,钢材的长度小于5米.*/
package  exec;

public   class  SteelSection  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        int i = 0;
        
float steel = 2000f;
        
while (steel >= 5...{
            i
++;
            steel 
= steel / 2;
            System.out.println(i 
+ "天后钢管长" + steel + "");
        }

    }


}

 

/** */ /**完全数是指其所有因子(包括1但不包括该数自身)的和等于该数,例如28=1+2++4+7+14,28就是一个完全数.
 * 编写一个程序求出2到10000之间的所有完全数.
*/

package  exec;

public   class  WanNumber  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        int m = 0;
        
for (int n = 2; n <= 10000; n++...{
            m 
= 0;
            
for (int i = 1; i < n; i++...{
                
if (n % i == 0...{
                    m 
+= i;
                    
if (m == n) ...{
                        System.out.println(n 
+ "个完全数");
                    }

                }

            }

        }

    }


}

 

/**/ /*编写一个Java类Prime,其中只有一个静态方法. boolean.isPrin=me(int n)//方法测试n是否是素数,
 * 如果是返回true 否则返回false  已知Babbage函数发f(x)=x*x+x+41,其中x为自然数,
 * 当x小于特定的n之间所有的函数均为素数,编写一个测试类,其中的main方法找出这个特定n值.
 
*/


package  exec;

public   class  Test  ... {

    
public static void main(String[] args) ...{
        
// TODO Auto-generated method stub
        int num = 200000// 表示1-num之间的自然数
        for (int i = 1; i <= num; i++...{
            
if (Prime.me(i)) ...{
                System.out.println(i 
+ "是素数");
            }
 else ...{
                System.out.println(i 
+ "不是素数");
            }

        }


    }


}

package  exec;

public   class  Prime  ... {
    
public static boolean me(int n) ...{
        
for (int x = 1; x < n; x++...{
            
if (n == x * x + x + 41...{
                
return true;
            }

        }

        
return false;
    }


}

 

 

 

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部