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;
    }


}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章