JAVA作业4

1素数和(5分)

题目内容:

我们认为2是第一个素数,3是第二个素数,5是第三个素数,依次类推。

现在,给定两个整数n和m,0

注意,是第n个素数到第m个素数之间的所有的素数,并不是n和m之间的所有的素数。


输入格式:

两个整数,第一个表示n,第二个表示m。


输出格式:

一个整数,表示第n个素数到第m个素数之间所有的素数的和,包括第n个素数和第m个素数。


输入样例:

2 4


输出样例:

15


代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int sum=0;
        int sqt=0;
        for(int i=1;i<20000;i++) {
            for(int j=2;j<=i;j++) {
                if(i%j==0&&i/j!=1) {
                    break;
                }
                if(j==i) {
                    sqt++;
                    if(n<=sqt&&m>=sqt) {
                        sum+=i;
                    }
                }
            }
        }
        System.out.println(sum);

    }

}

2念整数(5分)

题目内容:

你的程序要读入一个整数,范围是[-100000,100000]。然后,用汉语拼音将这个整数的每一位输出出来。

如输入1234,则输出:

yi er san si

注意,每个字的拼音之间有一个空格,但是最后的字后面没有空格。当遇到负数时,在输出的开头加上“fu”,如-2341输出为:

fu er san si yi


输入格式:

一个整数,范围是[-100000,100000]。


输出格式:

表示这个整数的每一位数字的汉语拼音,每一位数字的拼音之间以空格分隔,末尾没有空格。


输入样例:

-30


输出样例:

fu san ling


代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int inum = in.nextInt();
        int num = Math.abs(inum);
        int n1 = num / 100000;
        int n2 = num / 10000 - n1 * 10;
        int n3 = num / 1000 - n1 * 100 - n2 * 10;
        int n4 = num / 100 - n1 * 1000 - n2 * 100 - n3 * 10;
        int n5 = num / 10 - n1 * 10000 - n2 * 1000 - n3 * 100 - n4 * 10;
        int n6 = num % 10;
        if (inum < 0) {
            System.out.print("fu ");
        }
        if (num>99999) {
            switch (n1) {
            case 0:
                System.out.print("ling ");
                break;
            case 1:
                System.out.print("yi ");
                break;
            case 2:
                System.out.print("er ");
                break;
            case 3:
                System.out.print("san ");
                break;
            case 4:
                System.out.print("si ");
                break;
            case 5:
                System.out.print("wu ");
                break;
            case 6:
                System.out.print("liu ");
                break;
            case 7:
                System.out.print("qi ");
                break;
            case 8:
                System.out.print("ba ");
                break;
            case 9:
                System.out.print("jiu ");
                break;

            }
        }

        if (num>9999) {
            switch (n2) {
            case 0:
                System.out.print("ling ");
                break;
            case 1:
                System.out.print("yi ");
                break;
            case 2:
                System.out.print("er ");
                break;
            case 3:
                System.out.print("san ");
                break;
            case 4:
                System.out.print("si ");
                break;
            case 5:
                System.out.print("wu ");
                break;
            case 6:
                System.out.print("liu ");
                break;
            case 7:
                System.out.print("qi ");
                break;
            case 8:
                System.out.print("ba ");
                break;
            case 9:
                System.out.print("jiu ");
                break;

            }
        }
        if (num>999) {
            switch (n3) {
            case 0:
                System.out.print("ling ");
                break;
            case 1:
                System.out.print("yi ");
                break;
            case 2:
                System.out.print("er ");
                break;
            case 3:
                System.out.print("san ");
                break;
            case 4:
                System.out.print("si ");
                break;
            case 5:
                System.out.print("wu ");
                break;
            case 6:
                System.out.print("liu ");
                break;
            case 7:
                System.out.print("qi ");
                break;
            case 8:
                System.out.print("ba ");
                break;
            case 9:
                System.out.print("jiu ");
                break;

            }
        }
        if (num>99) {
            switch (n4) {
            case 0:
                System.out.print("ling ");
                break;
            case 1:
                System.out.print("yi ");
                break;
            case 2:
                System.out.print("er ");
                break;
            case 3:
                System.out.print("san ");
                break;
            case 4:
                System.out.print("si ");
                break;
            case 5:
                System.out.print("wu ");
                break;
            case 6:
                System.out.print("liu ");
                break;
            case 7:
                System.out.print("qi ");
                break;
            case 8:
                System.out.print("ba ");
                break;
            case 9:
                System.out.print("jiu ");
                break;

            }
        }
        if (num>9) {
            switch (n5) {
            case 0:
                System.out.print("ling ");
                break;
            case 1:
                System.out.print("yi ");
                break;
            case 2:
                System.out.print("er ");
                break;
            case 3:
                System.out.print("san ");
                break;
            case 4:
                System.out.print("si ");
                break;
            case 5:
                System.out.print("wu ");
                break;
            case 6:
                System.out.print("liu ");
                break;
            case 7:
                System.out.print("qi ");
                break;
            case 8:
                System.out.print("ba ");
                break;
            case 9:
                System.out.print("jiu ");
                break;

            }
        }
        if (num>=0) {
            switch (n6) {
            case 0:
                System.out.print("ling");
                break;
            case 1:
                System.out.print("yi");
                break;
            case 2:
                System.out.print("er");
                break;
            case 3:
                System.out.print("san");
                break;
            case 4:
                System.out.print("si");
                break;
            case 5:
                System.out.print("wu");
                break;
            case 6:
                System.out.print("liu");
                break;
            case 7:
                System.out.print("qi");
                break;
            case 8:
                System.out.print("ba");
                break;
            case 9:
                System.out.print("jiu");
                break;

            }
        }
    }
}

你可能感兴趣的:(JAVA作业4)