首页 > 代码库 > 问题1-5

问题1-5

1,生兔子问题(斐波那契数列)

public class Fibonacci {
public static void main(String[] args) {
    System.out.println("The rabit of 1th month : 1");
    System.out.println("The rabit of 2th month : 2");
    int f1=1,f2=1,f,m=24;
    for(int i=3;i<=m;i++){
        f=f2;
        f2=f1+f2;
        f1=f;
        System.out.println("The "+i+"th"+" month‘s rabit is "+f2);
    }
}
}

2,求100至201之间的素数:

public class primeNum {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 101; i < 200; i += 2) {
            boolean b = false;
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    b = false;
                    break;
                } else {
                    b = true;
                }
            }
            if (b == true) {
                count++;
                System.out.println("The prime number is " + i);
            }
        }
        System.out.println("The Number of prime number is " + count);
    }
}

 

3,水仙花数(自恋数):

public class narcissisticNum {
public static void main(String[] args) {
    int m,n,l;
    for(int k=101;k<1000;k++){
        m=k/100;
        n=k%100/10;
        l=k%10;
        if((m*m*m+n*n*n+l*l*l)==k){
            System.out.println(k+" is so Narcissitic!!!");
        }
    }
}
}

4,分解质因数:

import java.util.Scanner;

public class DecompositionOfTheQualityFactor {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input a int number: ");
        int n = scanner.nextInt();
        int k = 2;
        System.out.print(n + "=");
        while (k <= n) {
            if (k == n) {
                System.out.print(n);
                break;
            } else if (n % k == 0) {
                System.out.print(k + "*");
                n = n / k;
            } else {
                k++;
            }

        }
    }
}

5,成绩等级判断:

import java.util.Scanner;

public class ScoreGrade {
    public static void main(String[] args) {
        int x;
        char grade;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please input a score:");
        x = scanner.nextInt();
        grade = x >= 90 ? ‘A‘ : x >= 60 ? ‘B‘ : ‘C‘;
        System.out.println("The grade is: " + grade);
    }
}

问题1-5