Java基礎算法集50題


    最近因為要准備實習,還有一個藍橋杯的編程比賽,所以准備加強一下算法這塊,然后百度了一下java基礎算法,看到的都是那50套題,那就花了差不多三個晚自習的時間吧,大體看了一遍,做了其中的27道題,有一些實在是沒啥意思,也就不做了。下面我貼出源碼,如果大家嫌復制太麻煩,也可以在此篇下留言或是私信給我郵箱,我會發給你們。

    所以題可以參考http://blog.sina.com.cn/s/blog_60fafdda0100wb21.html,我還借鑒了它的函數命名,表示感謝,讓我想函數名是最頭疼的了。

           下面我貼出了我做的全部27道題。答案應該都是正確的,但是算法是否好,還有待商榷。都是很基礎的東西,如果想深入的話,可以看看算法系列15天速成,我也在學習中,可惜他是用C#實現的,多少有點語言障礙。

    【程序1 TestRabbit.java
    題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少? 
    1.
程序分析: 兔子的規律為數列1,1,2,3,5,8,13,21....

View Code
package cn.edu.hit;
/**
 * 兔子問題
 * 斐波那契數列求值
 * @author tonylp
 *題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,
 *小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?
 *1.程序分析: 兔子的規律為數列1,1,2,3,5,8,13,21....
 */
public class rabbit {
    public static final int MONTH = 15;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long f1 = 1L, f2 = 1L;
        long f;
        for(int i=3;i<MONTH;i++){
            f=f1+f2;
            f1=f2;
            f2=f;
            System.out.println("第"+i+"個月的兔子對數:"+f2);
        }
        /*
        for(int i =1 ; i<MONTH; i++){
            System.out.println("第"+i+"個月的兔子對數:"+fib(i));
        }
        */
    }
    //遞歸方法實現
    public static int fib(int month){
        if(month == 1 || month == 2){
            return 1;
        }else{
            return fib(month-1)+fib(month-2);
        }
    }
}

 

          【程序3FindDaffodilNumber.java
           題目:打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。例如: 
        153
是一個"水仙花數",因為153=1的三次方+5的三次方+3的三次方。 
        1.
程序分析:利用for循環控制100-999個數,每個數分解出個位,十位,百位。

View Code
package cn.edu.hit;

/**
 * 題目:打印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數本身。
 * 例如:153是一個"水仙花數",因為153=1的三次方+5的三次方+3的三次方。
 * 1.程序分析:利用for循環控制100-999個數,每個數分解出個位,十位,百位。
 * 
 * @author tonylp
 */
public class daffodils {
    public static void main(String[] args) {
        int a, b, c;
        int data;
        for (int i = 100; i < 999; i++) {
            a = i / 100;
            b = (i - 100 * a) / 10;
            c = i - 100 * a - 10 *b;
            data = a*a*a+b*b*b+c*c*c;
            if(data == i){
                System.out.println(i);
            }
        }
    }

}

 

           【程序4FenJie.java
    題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5 
    程序分析:對n進行分解質因數,應先找到一個最小的質數k,然后按下述步驟完成: 
    (1)
如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。 
    (2)
如果n<>k,但n能被k整除,則應打印出k的值,並用n除以k的商,作為新的正整數你n,重復執行第一步。 
    (3)
如果n不能被k整除,則用k+1作為k的值,重復執行第一步。
   

View Code
package cn.edu.hit;

/**
 * 任意整數分解 題目:將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。
 * 程序分析:對n進行分解質因數,應先找到一個最小的質數k,然后按下述步驟完成: (1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。
 * (2)如果n>k,但n能被k整除,則應打印出k的值,並用n除以k的商,作為新的正整數你n,重復執行第一步。
 * (3)如果n不能被k整除,則用k+1作為k的值,重復執行第一步。
 * 
 * @author tonylp
 */
public class resolveNum {
    public static final int NUM = 130;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = 2;
        int num = NUM;
        System.out.print(num + "=");
        while (num > k) {
            if (num % k == 0) {
                System.out.print(k + "*");
                num = num / k;
            } else {
                k++;
            }
        }
        System.out.println(k);
    }
}

 

            【程序6Test1.java GcdTest.java后者是輾轉相除法
    題目:輸入兩個正整數mn,求其最大公約數和最小公倍數。 
    1.
程序分析:利用輾除法。

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。
 *  1.程序分析:利用輾除法。
 * @author tonylp
 */
public class comonDivisor {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int first = a;
        int second = b;
        System.out.println("a=" + a + ";b=" + b);
        int temp;
        
        if (a < b) {
            temp = a;
            a = b;
            b = temp;
        }
        while (b != 0) {
                temp = a % b;
                a = b;
                b = temp;
            }
            System.out.println("最大公約數為"+a);
            System.out.println("最小公倍數為"+first*second/a);
        }
    }

  

            【程序7 StChar.java
    題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。 
    1.
程序分析:利用while語句,條件為輸入的字符不為'\n'.

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。 1.程序分析:利用while語句,條件為輸入的字符不為'\n'.
 * 
 * @author tonylp
 */
public class strIdentify {
    public static void main(String[] args) {
        int abcCount = 0;
        int spaceCount = 0;
        int numCount = 0;
        int otherCount = 0;
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (Character.isDigit(ch[i])) {
                numCount++;
            } else if (Character.isSpaceChar(ch[i])) {
                spaceCount++;
            } else if (Character.isLetter(ch[i])) {
                abcCount++;
            } else {
                otherCount++;
            }
        }
        System.out.println("字母個數"+abcCount);
        System.out.println("數字個數"+numCount);
        System.out.println("空格個數"+spaceCount);
        System.out.println("其他字符個數"+otherCount);
    }

}

  

           【程序8 TestAdd.java
    題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。 
    1.
程序分析:關鍵是計算出每一項的值。

View Code
package cn.edu.hit;
import java.util.Scanner;
/**
 * 題目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),
 * 幾個數相加有鍵盤控制。 1.程序分析:關鍵是計算出每一項的值。
 * 
 * @author tonylp
 */
public class numAdd {
    public static void main(String[] args) {
        System.out.println("請輸入數字0-9:");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        System.out.println("請輸入數字重復次數:");
        int b = sc.nextInt();
        int s = 0;
        System.out.println("a=" + a + ";b=" + b);
        for (int i = b; i > 0; i--) {
            s += i * a * Math.pow(10, (b - i));
        }
        System.out.println("s=" + s);
    }

}

  

    【程序9 WanShu.java
    題目:一個數如果恰好等於它的因子之和,這個數就稱為"完數"。例如6=123.編程 找出1000以內的所有完數。
 

View Code
package cn.edu.hit;

/**
 * 題目:一個數如果恰好等於它的因子之和,這個數就稱為"完數"。 例如6=1+2+3.編程 找出1000以內的所有完數。
 * 
 * @author tonylp
 */
public class wanShu {
    public static void main(String[] args) {
        int k = 2;
        int num = 0;
        int temp = 1;
        int j = 0;
        for (num = 1; num <= 1000; num++) {
            k = 2;
            temp = 1;
            j = num;
            while (j >= k) {
                if (j % k == 0) {
                    temp += k;
                    j = j / k;
                } else {
                    k++;
                }
            }
            if (temp == num) {
                System.out.println(temp);
            }
            
        }
    }
}

  

    【程序10TestBall.java
    題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米?第10次反彈多高?

View Code
package cn.edu.hit;
/**
 * 題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;
 * 再落下,求它在第10次落地時,共經過多少米?第10次反彈多高?
 */
public class testBall {
    public static void main(String[] args) {
        double a = 100;
        double sum = 100;
        for(int i =2 ;i<=10;i++){
            a = a*0.5;
            sum += a*2;
        }
        System.out.println("a="+a);
        System.out.println("sum="+sum);
    }
}

 

    【程序11 TestTN.java
    題目:有1234個數字,能組成多少個互不相同且無重復數字的三位數?都是多少? 
    1.
程序分析:可填在百位、十位、個位的數字都是1234。組成所有的排列后再去 掉不滿足條件的排列。
 

View Code
package cn.edu.hit;

/**
 * 題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少? 1.程序分析:可填在百位、十位、個位的數字都是1、2、3、4。
 * 組成所有的排列后再去 掉不滿足條件的排列。
 * 
 * @author tonylp
 */
public class testTN {
    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = 0;
        int num = 1;
        for (a = 1; a < 5; a++) {
            for (b = 1; b < 5; b++) {
                for (c = 1; c < 5; c++) {
                    if (a != b && a != c && b != c) {
                        System.out.println("NO." + num + ":" + a + "" + b + ""
                                + c);
                        num++;
                    }
                }
            }
        }
    }

}

 

    【程序12 MoneyAward.java
    題目:企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提        成7.5%20萬到40萬之間時,高於20萬元的部分,可提成5%40萬到60萬之間時高於40萬元的部分,可提成3%60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高     於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數? 
    1.
程序分析:請利用數軸來分界,定位。注意定義時需把獎金定義成長整型。

View Code
package cn.edu.hit;
import java.util.Scanner;
/**
 * 題目:企業發放的獎金根據利潤提成。 利潤(I)低於或等於10萬元時,獎金可提10%; 利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,
 * 高於10萬元的部分,可可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%; 40萬到60萬之間時高於40萬元的部分,可提成3%;
 * 60萬到100萬之間時,高於60萬元的部分,可提成1.5%, 高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?
 * 1.程序分析:請利用數軸來分界,定位。注意定義時需把獎金定義成長整型。
 * 
 * @author tonylp
 */
public class moneyAward {
    public static void main(String[] args) {
        double rate1 = 0.1, rate2 = 0.075, rate3 = 0.05, rate4 = 0.03, rate5 = 0.014, rate6 = 0.01;
        long reward = 0;
        System.out.println("請輸入利潤:");
        Scanner sc = new Scanner(System.in);
        long money = sc.nextLong();
        if (money >= 0 && money <= 100000) {
            reward = (long) (money * rate1);
        } else if (money <= 200000) {
            reward = (long) (100000 * rate1 + (money - 100000) * rate2);
        } else if (money <= 400000) {
            reward = (long) (100000 * rate1 + 100000 * rate2 + (money - 200000) * rate3);
        } else if (money <= 600000) {
            reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3
                    + (money - 400000) * rate4);
        } else if (money <= 1000000) {
            reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3 + 200000
                    * rate4 + (money - 6000000) * rate5);
        } else {
            reward = (long) (10000 * rate1 + 100000 * rate2 + 200000 * rate3 + 200000
                    * rate4 + 400000 * rate5 + (money - 1000000) * rate6);
        }
        System.out.println("獎金為:"+reward);
    }
}

  

    【程序13FindNumber.java
    題目:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少? 
    1.
程序分析:在10萬以內判斷,先將該數加上100后再開方,再將該數加上268后再開方,如果開方后的結果滿足如下條件,即是結果。請看具體分析

View Code
package cn.edu.hit;
/**
 * 題目:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少? 
 * 1.程序分析:在10萬以內判斷,先將該數加上100后再開方,
 * 再將該數加上268后再開方,如果開方后的結果滿足如下條件,即是結果。
 * 請看具體分析:
 * @author tonylp
 */
public class findNumber {
    public static void main(String[] args) {
        for(int i=0;i<100000;i++){
            if((Math.sqrt(i+100)%1==0)&&(Math.sqrt(i+168)%1 == 0)){
                System.out.println(i);
            }
        }
    }
}

  

    【程序14 TestDay.java
    題目:輸入某年某月某日,判斷這一天是這一年的第幾天? 
    1.
程序分析:以35日為例,應該先把前兩個月的加起來,然后再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大於3時需考慮多加一天。
 

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:輸入某年某月某日,判斷這一天是這一年的第幾天? 1.程序分析:以3月5日為例,應該先把前兩個月的加起來, 然后再加上5天即本年的第幾天,
 * 特殊情況,閏年且輸入月份大於3時需考慮多加一天。
 * 
 * @author tonylp
 */
public class testDay {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入年份:");
        int year = sc.nextInt();
        System.out.println("請輸入月份:");
        int month = sc.nextInt();
        System.out.println("請輸入天數:");
        int day = sc.nextInt();
        int date = 0;
        int arr[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
            arr[1] = 29;
        }
        for (int i = 0; i < month - 1; i++) {
            date += arr[i];
        }
        date += day;
        System.out.println("天數為:" + date);
    }

}

  

    【程序15TestCompare.java
    題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。 
    1.
程序分析:我們想辦法把最小的數放到x上,先將xy進行比較,如果x>y則將xy的值進行交換,然后再用xz進行比較,如果x>z則將xz的值進行交換,這樣能使x最小 

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:輸入三個整數x,y,z,請把這三個數由小到大輸出。 
 * 1.程序分析:我們想辦法把最小的數放到x上,
 * 先將x與y進行比較,如果x>y則將x與y的值進行交換,
 * 然后再用x與z進行比較,如果x>z則將x與z的值進行交換,這樣能使x最小。
 * @author tonylp
 */
public class testCompare {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入x:");
        int x = sc.nextInt();
        System.out.println("請輸入y:");
        int y = sc.nextInt();
        System.out.println("請輸入z:");
        int z = sc.nextInt();
        int temp;
        if(x>y){
            temp = x;
            x = y;
            y= temp;
        }
        if(x>z){
            temp = x;
            x = z;
            z = temp;
        }
        if(y>z){
            temp = y;
            y = z;
            z = temp;
        }
        System.out.println("從小到大的順序為:"+x+"<"+y+"<"+z);
    }

}

 

    【程序16Nine.java
    題目:輸出9*9口訣。 
    1.
程序分析:分行與列考慮,共99列,i控制行,j控制列。

View Code
package cn.edu.hit;
/**
 * 題目:輸出9*9口訣。 
 * 1.程序分析:分行與列考慮,共9行9列,i控制行,j控制列。
 * @author tonylp
 */
public class nine {
    public static void main(String[] args) {
        for(int i = 1;i<=9;i++){
            for(int j = 1;j<=i;j++){
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println("");
        }
    }

}

 

    【程序17MonkeyEatPeach.java
    題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個 第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天      剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。 
    1.
程序分析:采取逆向思維的方法,從后往前推斷。
 

View Code
package cn.edu.hit;
/**
 * 題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當即吃了一半,還不癮,又多吃了一個
 *  第二天早上又將剩下的桃子吃掉一半,又多吃了一個
 *  。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。 
 * 1.程序分析:采取逆向思維的方法,從后往前推斷。
 * @author tonylp
 */
public class monkeyEatPeach {
    public static void main(String[] args) {
        int peach = 1;
        for(int i =0; i<10;i++){
            peach = (peach+1)*2;
        }
        System.out.println(peach);
    }
}

 

    【程序19LingXing.java 
    題目:打印出如下圖案(菱形) 
    *
    ***
    ******
    ********
     ******
         ***
           *
    1.
程序分析:先把圖形分成兩部分來看待,前四行一個規律,后三行一個規律,利用雙重 for循環,第一層控制行,第二層控制列。

View Code
package cn.edu.hit;

/**
 * 題目:打印出如下圖案(菱形) * *** ****** ******** ****** *** *
 * 1.程序分析:先把圖形分成兩部分來看待,前四行一個規律, 后三行一個規律,利用雙重 for循環, 第一層控制行,第二層控制列。
 * 
 * @author tonylp
 */
public class lingXing {
    public static void main(String[] args) {
        int[] arr = { 1, 3, 6, 8, 6, 3, 1 };
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < arr[i]; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        for (int i = 4; i < arr.length; i++) {
            for (int j = arr[i]; j < 8; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < arr[i]; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }

}

 

    【程序20TestAdd2.java
    題目:有一分數序列:2/13/25/38/513/821/13...求出這個數列的前20項之和。 
    1.
程序分析:請抓住分子與分母的變化規律。

View Code
package cn.edu.hit;

/**
 * 題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和。 1.程序分析:請抓住分子與分母的變化規律。
 * 
 * @author tonylp
 */
public class testAdd2 {
    public static void main(String[] args) {
        int[] a = new int[20];
        int[] b = new int[20];
        double sum = 0.0;
        a[0] = 2;
        a[1] = 3;
        b[0] = 1;
        b[1] = 2;
        for(int i = 2;i<20;i++){
            a[i] = a[i-1]+a[i-2];
        }
        for(int j =2;j<20;j++){
            b[j] = b[j-1]+b[j-2];
        }
        for(int i =0;i<20;i++){
            sum+=a[i]/b[i];
        }
        System.out.println(sum);
    }

}

 

    【程序21TestJieCheng.java
    題目:求1+2!+3!+...+20!的和 
    1.
程序分析:此程序只是把累加變成了累乘。

View Code
package cn.edu.hit;
/**
 * 題目:求1+2!+3!+...+20!的和 
 * 1.程序分析:此程序只是把累加變成了累乘。
 * @author tonylp
 */
public class testJieCheng {
    public static void main(String[] args) {
        int sum = 0;
        int data = 1;
        for(int i = 1;i<=20;i++){
            data = data*i;
            sum += data;
        }
        System.out.println(sum);
    }
}

  

    【程序22 
    題目:利用遞歸方法求5! TestJieCheng.java
    1.
程序分析:遞歸公式:fn=fn_1*4!

View Code
package cn.edu.hit;
/**
 * 題目:利用遞歸方法求5!
 * 1.程序分析:遞歸公式:fn=fn_1*4!
 * @author tonylp
 */
public class testDiGuiJieCheng {
    public static void main(String[] args) {
        int sum = jieCheng(5);
        System.out.println(sum);
    }
    public static int jieCheng(int n){
        if(n>0){
            return jieCheng(n-1)*n;
        }else{
             return  1;
        }
    }

}

 

    【程序23TestAge.java
    題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大      兩歲。最后問第一個人,他說是10歲。請問第五個人多大? 
    1.
程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲數,需知道第四人的歲數,依次類推,推到第一人(10歲),再往回推。

View Code
package cn.edu.hit;
/**
 * 題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數,他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。
 * 問第2個人,說比第一個人大兩歲。
 * 最后問第一個人,他說是10歲。
 * 請問第五個人多大? 
 * 1.程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。
 * 要想知道第五個人歲數,需知道第四人的歲數,依次類推,推到第一人(10歲),再往回推。
 * @author tonylp
 */
public class testAge {
    public static void main(String[] args) {
        System.out.println(age(5));
    }
    public static int age(int n){
        int c ;
        if(n == 1){
            c = 10;
        }else{
            c = age(n-1)+2;
        }
        return c;
    }
}

 

    【程序24TestNumber.java
    題目:給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。

View Code
package cn.edu.hit;
/**
 * 題目:給一個不多於5位的正整數,要求:一、求它是幾位數,二、逆序打印出各位數字。
 * 
 * @author tonylp
 */
import java.util.Scanner;
public class testNumber {
    public static void main(String[] args) {
        System.out.println("請輸入一個正整數:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[] a = new int[5];
        int b = 0;
        int n = 0;      
        for (int i = 4; i >= 0; i--) {
            b = (int) Math.pow(10, i);
            if(num/b != 0&&i>=n){
                n = i+1;             //位數=最高位+1
            }
                a[i] = num / b;
                num = num - a[i]*b;
        }
        System.out.println("位數:"+n);
        for(int j =0;j< n;j++){
            if(a[j]!=-1){
                System.out.print(a[j]+"\t");
            }
        }
        System.out.println("");
        //test();
    }
    //另一種簡單方法
    /*
    public static void test(){
        System.out.println("請輸入一個正整數:");
        Scanner sc = new Scanner(System.in);
        String num = sc.nextLine();
        int numLength = num.length();
        System.out.println("位數:"+numLength);
        for(int i = numLength-1; i>=0;i--){
            System.out.print(num.charAt(i)+"\t");
        }
    }
    */
}

 

    【程序27 SuShu.java
    題目:求100之內的素數

View Code
package cn.edu.hit;
/**
 * 題目:求100之內的素數 
 * @author tonylp
 *
 */
public class suShu {
    public static void main(String[] args) {
        int j = 0 ;
        int flag = 0;
        for(int i=2;i<100;i++){
            j = (int) (Math.sqrt(i));
            for(int k = 2; k<=j;k++){
                if(i%k == 0){
                    flag = 1;
                }
            }
            if(flag == 0){
                System.out.println(i);
            }
            flag = 0;
        }
    }
}

 

    【程序33YangHui.java
    題目:打印出楊輝三角形(要求打印出10行如下圖) 
    1.
程序分析: 
         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
    1 5 10 10 5 1

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:打印出楊輝三角形(要求打印出10行如下圖) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
 * 現在打印出的格式不是很好看。
 * @author tonylp
 * 
 */
public class yanghui {
    public static void main(String[] args) {
        System.out.println("請輸入行數:");
        Scanner sc = new Scanner(System.in);
        int line = sc.nextInt();
        int[] a = new int[line];
        for (int i = 0; i < line; i++) {
            a[i] = 1;
        }
        if (line == 1) {
            System.out.println(1);
        } else if (line == 2) {
            System.out.println(1);
            System.out.println(1 + "\t" + 1);
        } else {
            System.out.println(1);
            System.out.println(1 + "\t" + 1);
            for (int i = 1; i < line-1; i++) {
                for (int j = i; j >= 1; j--) {
                    a[j] = a[j] + a[j - 1];
                }
                for(int k =0;k<i+2;k++){
                    System.out.print(a[k]+"\t");
                }
                System.out.println();
            }
        }
    }
}

 

    【程序37 Test3Quit.java
    題目:有n個人圍成一圈,順序排號。從第一個人開始報數(從13報數),凡報到3的人退出圈子,問最后留下的是原來第幾號的那位。
  

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:有n個人圍成一圈,順序排號。從第一個人開始報數(從1到3報數),凡報到3的人退出圈子, 問最后留下 的是原來第幾號的那位。
 * 
 * @author tonylp
 * 
 */
public class test3Quit {
    public static void main(String[] args) {
        System.out.println("請輸入n:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        int i = 0;
        int t = 0;
        while (n > 1) {
            if (a[i] == 0) {
                t++;
                if (t == 3) {
                    t = 0;
                    a[i] = 1;
                    n--;
                }
            }
            i++;
            if(i == a.length){
                i = 0;
            }
        }
        for(int j=0;j<a.length;j++){
            if(a[j]!=1){
                System.out.println(j+1);
            }    
        }
    }
}

 

    【程序41 MonkeyPeach.java
    題目:海灘上有一堆桃子,五只猴子來分。第一只猴子把這堆桃子憑據分為五份,多了一個,這只猴子把多的一 
    個扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中 
    ,拿走了一份,第三、第四、第五只猴子都是這樣做的,問海灘上原來最少有多少個桃子?

View Code
package cn.edu.hit;

/**
 * 題目:海灘上有一堆桃子,五只猴子來分。第一只猴子把這堆桃子憑據分為五份,多了一個,這只猴子把多的一
 * 個扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中 ,
 * 拿走了一份,第三、第四、第五只猴子都是這樣做的,問海灘上原來最少有多少個桃子?
 * 
 * @author tonylp
 * 
 */
public class monkeyPeach {
    public static void main(String[] args) {
            int monkey = 0;
            int a =getPeach(monkey);   
            System.out.println(a);
    }
    public static int getPeach(int monkey) {
        if (monkey <5) {
            return (getPeach(monkey+1)*5+1);
        }else{
            return 1;
        }
    }
}

 

    【程序44 TestEven.java
    題目:一個偶數總能表示為兩個素數之和。

View Code
package cn.edu.hit;

import java.util.Scanner;

/**
 * 題目:一個偶數總能表示為兩個素數之和。
 * 
 * @author tonylp
 */
public class testEven {
    public static void main(String[] args) {
        System.out.println("請輸入一個偶數:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int j = 0;
        int num2 = 0;
        int flag = 0;
        int tag = 0;
        int temp = 0;
        if (num % 2 != 0 || num == 0) {
            System.out.println("輸入數據錯誤!");
        } else {
            for (int i = 3; i < num; i++) {
                j = (int) Math.sqrt(i);
                for (int k = 2; k <= j; k++) {
                    if (i % k == 0) {
                        flag = 1;
                    }
                }
                if (flag == 0) {
                    num2 = num - i;
                    temp = (int) Math.sqrt(num2);
                    for (int k = 2; k <= temp; k++) {
                        if (num2 % k == 0) {
                            tag = 1;
                        }
                    }
                    if (tag == 0&&num2>=3) {
                        System.out.println(num + "=" + i + "+" + num2);
                    }
                    tag = 0;
                }
                flag = 0;
            }
        }
    }
}

    抱怨一句,這個代碼重疊用的我好揪心,老是出現代碼沒法打開了。。

   以上全部都屬個人原創,請大家轉載的時候附上原創鏈接: http://www.cnblogs.com/tonylp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM