PTA46题(Java基础练习)

Java作业练习(1)

这学期Java课的第一次作业嗷
(做完再删掉这句话)
我觉得可以

7-1 写一个程序,输出你的姓名、年龄、所在学院、所在专业、所在班级

写一个程序,输出你的姓名、年龄、所在学院、所在专业、所在班级。

输出样例:
姓名:张三
年龄:20
所在学院:计算机学院
所在专业:计算机科学与技术
所在班级:1班

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            String n, m1, m2;
            int old, m3;
            n = scanner.nextLine();
            old = Integer.parseInt(scanner.nextLine());
            m1 = scanner.nextLine();
            m2 = scanner.nextLine();
            m3 = Integer.parseInt(scanner.nextLine());
            System.out.println("姓名:" + n + "\n" +
                    "年龄:" + old + "\n" +
                    "所在学院:" + m1 + "\n" +
                    "所在专业:" + m2 + "\n" +
                    "所在班级:" + m3 + "班");
        }else {
            System.out.println("姓名:张三\n" +
                    "年龄:20\n" +
                    "所在学院:计算机学院\n" +
                    "所在专业:计算机科学与技术\n" +
                    "所在班级:1班");
        }
    }
}
7-2 jmu-Java-02基本语法-05-浮点数的精确计算

输入若干对浮点数,对每对浮点数输出其精确的和与乘积。
以下输入样例为两对浮点数输入,实际上有可能有不定对数的浮点数需要输入计算。

注1:直接使用double类型数据进行运算,无法得到精确值。
注2:输出时直接调用BigDecimal的toString方法。

输入样例:

69.1 0.02
1.99 2.01

输出样例:

69.12
1.382
4.00
3.9999

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BigDecimal a,b;
        while (scanner.hasNext()){
            a = scanner.nextBigDecimal();
            b = scanner.nextBigDecimal();
            System.out.println(a.add(b));
            System.out.println(a.multiply(b));
        }
    }
}
7-3 jmu-Java-02基本语法-06-枚举

定义一个枚举类型Grade来表示考试的4个等级,值包含A,B,C,D,E

编写一个函数Grade getGrade(int score)将传递进来的score转化为枚举类型

score>=90 and <=100返回A,
score>=80 and <90 返回B,
score>=70 and <80 返回C,
score>=60 and <70返回D,
其他的返回E
main方法
输入分数后,调用getGrade函数返回相应的Grade,使用switch根据Grade,根据分数打印不同的评价:
Excellent Good Average Fair Poor
并且每输出一个评语后,要调用如下函数

public static void printGradeInfo(Grade grade){
       System.out.println("class name="+Grade.class);
       System.out.println("grade value="+grade);
}

输入样例:

90
80
70
60

输出样例:

Excellent
class name=class Grade
grade value=A
Good
class name=class Grade
grade value=B
Average
class name=class Grade
grade value=C
Fair
class name=class Grade
grade value=D

import java.util.Scanner;

enum  Grade{
    A,B,C,D,E
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m;
        while (scanner.hasNext()){
            m = scanner.nextInt();
            printGradeInfo(getGrade(m));
        }
    }
    public static Grade getGrade(int score){
        if(score>=90&&score<=100){
            System.out.println("Excellent");
            return Grade.A;
        }
        else if(score>=80&&score<90){
            System.out.println("Good");
            return Grade.B;
        }
        else if(score>=70&&score<80){
            System.out.println("Average");
            return Grade.C;
        }
        else if(score>=60&&score<70){
            System.out.println("Fair");
            return Grade.D;
        }
        else {
            System.out.println("Poor");
            return Grade.E;
        }
    }
    public static void printGradeInfo(Grade grade){
        System.out.println("class name="+Grade.class);
        System.out.println("grade value="+grade);
    }
}

7-4 jmu-Java-02基本语法-07-大整数相加 (4 分)

有若干大整数,需要对其进行求和操作。

输入格式
每行输入一个字符串代表一个大整数,连续输入若干行,当某行字符为e或E时退出。

输入样例:

42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
e

输出样例:

97967883730498271109594004638709798272918548148116


import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String n;
        BigDecimal bigDecimal,sum = BigDecimal.valueOf(0L);
        while (scanner.hasNext()){
            n = scanner.nextLine();
            if ("e".equals(n) || "E".equals(n)){
                break;
            }
            bigDecimal = new BigDecimal(n);
            sum = sum.add(bigDecimal);
        }
        System.out.println(sum);
    }
    
}

7-5 testestjava

这是一个编程题模板。请在这里写题目描述。例如:本题目要求读入2个整数A和B,然后输出它们的和。

输入格式:
请在这里写输入格式。例如:输入在一行中给出2个绝对值不超过1000的整数A和B。

输出格式:
请在这里描述输出格式。例如:对每一组输入,在一行中输出A+B的值。

输入样例:

在这里给出一组输入。例如:
18 -299

输出样例:

在这里给出相应的输出。例如:
-281

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String shuru = scanner.nextLine();
        String n = scanner.nextLine();
        String[] s = n.split(" ");
        System.out.println("在这里给出相应的输出。例如:");
        System.out.println(Integer.parseInt(s[0]) + Integer.parseInt(s[1]));
    }
    
}
7-6 字符串处理

编写一个程序,用户输入任意一个字符串,显示它的长度和第一个字符。

输入格式:
输入任意一个字符串。

输出格式:
显示它的长度和第一个字符,其间用,分隔。

输入样例:

abc 4567

输出样例:

8,a

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String strings = scanner.nextLine();
        System.out.println(strings.length() + "," + strings.charAt(0));
    }
    
}
7-7 十进制转十六进制

编写一个程序,提示用户输入0~15之间的一个整数,显示其对应的十六进制数。

输入格式:
输入一个0~15之间的整数。

输出格式:
若输入的整数在0~15范围内,则输出对应的十六进制数。否则输出“Invalid input”。

输入样例:

11

输出样例:

B

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char m = 'A';
        int n = scanner.nextInt();
        if(n > 15) {
            System.out.println("Invalid input");
        }else if (n >= 10 && n <= 15){
            System.out.println((char) (m+n-10));
        }else {
            System.out.println(n);
        }
        
    }
    
}
7-8 求最大值

本题目要求读入2个整数A和B,然后输出两个数的最大值。

输入格式:
输入在一行中给出2个绝对值不超过1000的整数A和B。

输出格式:
对每一组输入,在一行中输出最大值。

输入样例:
在这里给出一组输入。例如:

18 -299

输出样例:
在这里给出相应的输出。例如:

18


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int A = scanner.nextInt();
        int B = scanner.nextInt();
        if(A > B)
            System.out.println(A);
        else
            System.out.println(B);
        scanner.close();
    }
    
}

7-9 jmu-java-01入门-基本输入

对输入的若干行(每行以空格或者多个空格)数字字符串求和并输出。

输入格式:
每行包含两个数字字符串,中间以一个或者多个空格分隔。

输出格式:
输出两个数的和

输入样例:

1 1
2 3
-100 100
-100 -100
10 0

输出样例:
在这里给出相应的输出。例如:

2
5
0
-200
10


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()){
            String s,a = "",b="";
            int sum = 0;
            s = scanner.nextLine();
            int i,j = 0;
            boolean f= false;
            for (i = 0; i < s.length(); i++) {
                if (s.charAt(i) != ' ') {
                    a = a + s.charAt(i);
                    f = true;
                }else if (f == true) {
                    j = i;
                    break;
                }
            }

            for (j = i; j < s.length(); j++) {
                if (s.charAt(j) != ' '){
                    b+=s.charAt(j);
                }
            }

            System.out.println(Integer.parseInt(a) + Integer.parseInt(b));
        }
    }
    
}

7-10 编程题:统计符合条件元素的个数-hebust

统计1…n的闭区间中,能够被3整除元素的奇数和偶数的个数

输入格式:
输入值n的范围是 【1…1000】

输出格式:
奇数个数,偶数个数

输入样例:

5

输出样例:

1,0

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a = 0,b = 0;
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 2 == 0){
                b++;  
            }
            if (i % 3 == 0 && i % 2 != 0){
                a++;
            }
        }
        System.out.println(a+","+b);
    }
    
}

7-11 编程题:判断闰年-hebust

根据输入的正整数y所代表的年份,计算输出该年份是否为闰年 闰年的判断标准:

能够被4整除且不能被100整除的年份

或者能够被400整除的年份

输入格式:
输入n取值范围是 【1…3000】

输出格式:
是闰年,输出 yes

非闰年,输出 no

输入样例:
在这里给出一组输入。例如:

100

输出样例:
在这里给出相应的输出。例如:

no


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int y = scanner.nextInt();
    if (((y % 4 == 0 )&& (y % 100 != 0)) || y % 400 == 0) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }
    }
}


7-12 输出月份

分别用if、switch两种方法编写根据0~11的整数转换为十二月份的程序片断,假定数字0对应一月份。

输入格式:
每一行输入0~12之间的一个数字

输出格式:
输出该数字对应的月份

输入样例:

0

输出样例:

请输入月份:
2
三月份
请输入月份:
12
请重新输入月份


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        if (n >= 12){
            System.out.println("请重新输入月份");
        }else {
            String[] strings = {"一月份", "二月份", "三月份", "四月份", "五月份", "六月份",
                    "七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份"};
            System.out.println(strings[n]);
        }
    }
    
}

7-13 点是否在圆内?

编写程序,提示用户输入一个点(x,y),然后检查这个点是否在以原点(0,0)为圆心、半径为10的圆内。

输入格式:
输入任意一个点的x轴和y轴坐标值,且两个值之间空格分隔。

输出格式:
若点在圆内,输出1,否则输出0。

输入样例:

4 5

输出样例:

1


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        if( x * x + y * y <= 100) {
            System.out.println("1");
        } else {
            System.out.println("0");
        }
    }
    
}

7-14 给出一个月的总天数

编写程序,提示用户输入月份和年份,然后显示这个月的天数。

输入格式:
输入任意符合范围(1月12月)的月份和(1900年9999年)年份,且两个值之间空格分隔。

输出格式:
输出给定年份和月份的天数。

输入样例:

2 2000

输出样例:

29


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int month = scanner.nextInt();
        int year = scanner.nextInt();
        int day = 0;
    if (month != 2) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    day = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day = 30;
            }
            }else if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0) {
                day = 29;
            } else {
                day = 28;
            }
    System.out.println(day);
}
}
   
7-15 程序改错题1

程序改错题。以下代码目标是实现从键盘输入1个整数x,然后根据x的值做不同的计算,输出结果。(程序有错,请改正后提交)

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int x, j = 1;
x = in.nextInt();
switch (x) {
case 1:
j++;
case 2:
j++;
case 3:
j = x*j++;
case 4:
j= x + j++;
case 5:
j = x - j++;
default:
j = x;
}
System.out.println(j);
}
}

输入格式:
输入整数x。

输出格式:
输出值。

输入样例:

5

输出样例:

4


import java.util.Scanner;

public class Main {
        public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int x, j = 1;
        x = in.nextInt();
        switch (x) {
            case 1:
                j++;break;
            case 2:
                j++;break;
            case 3:
                j = x*j++;break;
            case 4:
                j= x + j++;break;
            case 5:
                j = x - j++;break;
            default:
                j = x;break;
        }
        System.out.println(j);
    }
    
}

7-16 根据和找到最大乘积

试求和为N,积为最大的两个整数数分别是多少。

从键盘中输入一个整数,它是另外两个整数的加法运算和,这样的两个整数的组合有很多种,请找出乘积最

大的一组数据。请注意输出两个整数以空格分割,按由小到大的顺序输出。

输入格式:
从键盘中输入一个整数

输出格式:
在一行中输出两个整数,以空格分割,按由小到大的顺序输出。

输入样例:

33

输出样例:

16 17

输入样例:

-51

输出样例:

-26 -25


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int a;
        a = scanner.nextInt();
        if (a == 0){
            System.out.println(-1+" "+1);
        }else {
            if (a % 2 == 0) {
                System.out.println(a / 2 + " " + a / 2);
            } else {
                System.out.println((a - 1) / 2 + " " + (a + 1) / 2);
            }
        }
    }
    
}

7-17 编程题:数据的规范化处理问题-hebust

在进行数据处理时,输入数据未经过滤,存在一些不符合要求的数据 要求编写数据处理程序,小于0的数据一律指定为0,大于100的数据一律指定为100

输入格式:
输入:所有元素占一行,元素之间使用空格分开,元素均为整数,范围【-300…300】

输出格式:
输出:所有元素占一行,元素之间使用西文逗号分开,最后一个元素末尾保留西文逗号

输入样例:
在这里给出一组输入。例如:

-1 10 105

输出样例:
在这里给出相应的输出。例如:

0,10,100,


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] strings = s.split(" ");
        for (int i = 0; i < strings.length; i++) {
            if (Integer.parseInt(strings[i]) < 0){
                System.out.print(0+",");
            }else if (Integer.parseInt(strings[i]) > 100){
                System.out.print(100+",");
            }else {
                System.out.print(strings[i]+",");
            }
        }
    }
    
}

7-18 编程题:复杂情况下的数据统计问题-hebust

复杂环境下的数据统计问题

进行数值统计的时候,可能会混入一些非数值的元素,下面请编程完成对输入序列进行求和统计并输出, 如果遇到非数值元素,则自动跳过,并在最终结果输出行之后另起一行,输出attention

输入格式:
单行输入,元素之间使用空格分开

输出格式:
对元素所对应的整数进行求和并输出

如果遇到非数值元素,则自动跳过,并在最终结果输出行之后另起一行,输出attention

输入样例a:
在这里给出一组输入。例如:

1 2 3 4 5

输出样例a:
在这里给出相应的输出。例如:

15

输入样例b:
在这里给出一组输入。例如:

1 2 3 4 a 5

输出样例b:
在这里给出相应的输出。例如:

15
attention


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] strings = s.split(" ");
        int sum = 0;
        boolean f = false;
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].charAt(0)=='-'){
                sum += Integer.parseInt(strings[i]);
            }else if (strings[i].charAt(0)>='0' && strings[i].charAt(0) <= '9'){
                try {
                    sum += Integer.parseInt(strings[i]);
                }catch (Exception a){
                    f = true;
                    continue;
                }
            }else {
                f = true;
            }
        }
        System.out.println(sum);
        if (f){
            System.out.println("attention");
        }
    }
}

7-19 二进制的前导的零

计算机内部用二进制来表达所有的值。一个十进制的数字,比如18,在一个32位的计算机内部被表达为00000000000000000000000000011000。可以看到,从左边数过来,在第一个1之前,有27个0。我们把这些0称作前导的零。

现在,你的任务是写一个程序,输入一个整数,输出在32位表达下它前导的零的个数。

输入格式:
一个整数,在32位的整数可以表达的范围内。

输出格式:
一个整数,表达输入被表达为一个32位的二进制数时,在第一个1之前的0的数量。

输入样例:

256

输出样例:

23


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        Integer a = scanner.nextInt();
        if (a.equals(0)){
            System.out.println(32);
        }else {
            String s = Integer.toBinaryString(a);
            System.out.println(32 - s.length());
        }
    }
}

7-20 JAVA-水仙花数

水仙花数是指一个N位正整数(7≥N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=13+53+33。 要求编写程序,计算所有N位水仙花数。

输入格式:
输入一个正整数N(3≤N≤7)。

输出格式:
按递增顺序输出所有N位水仙花数,每个数字占一行。

输入样例:
在这里给出一组输入。例如:

3

输出样例:
在这里给出相应的输出。例如:

153
370
371
407


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int i,j,a,b,c;
        for(i =100;i<1000;i++) {
          a=i/100;
          b=i%10;
          c=(i/10)%10;
          if(Math.pow(a,N) + (Math.pow(b,N)) + (Math.pow(c, N))==(i))
            System.out.println(i);
        }
}
}

7-21 JAVA-求整数序列中出现次数最多的数

要求统计一个整型序列中出现次数最多的整数及其出现次数。

输入格式:
在一行中给出序列中整数个数N(0<N≤1000),依次给出N个整数,每个整数占一行。

输出格式:
在一行中输出出现次数最多的整数及其出现次数,数字间以空格分隔。题目保证这样的数字是唯一的。

输入样例:
在这里给出一组输入。例如:

10
3
2
-1
5
3
4
3
0
3
2

输出样例:
在这里给出相应的输出。例如:

3 4


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner (System.in);
        int n = scanner.nextInt();
        int b = 0;
        int t = 0;
        int[] arr = new int[1000];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        for(int i = 0;i < n;i ++){
            int c = 0;
            for(int j = 0;j < n;j ++){
                if(arr[i] == arr[j]){
                    c ++;
                }
            }
            if(c > b){
                t = arr[i];
                b = c;
            }
    }
    System.out.println(t + " " + b);
    }
    
}

7-22 统计正数和负数的个数然后计算这些数的平均值

编写程序,输入未指定个数的整数,判断读入的正数有多少个,读入的负数有多少个,然后计算这些输入值得总和及平均值(不对0计数)。当输入为0时,表明程序结束。将平均值以double型数据显示。

输入格式:
输入在一行中给出一系列整数,其间以空格分隔。当读到0时,表示输入结束,该数字不要处

输出格式:
在第一行中输出正整数的个数; 在第二行中输出负整数的个数; 在第三行中输出这些输入值的总和(不对0计数); 在第四行中输出这些输入值的平均值(double型数据)。

输入样例:

1 2 -1 3 0

输出样例:

3
1
5
1.25


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        if (s.equals("0")){
            return;
        }
        String[] s1 = s.split(" ");
        int[] o=new int[s1.length];
        for (int i=0;i<s1.length;i++){
            o[i]=Integer.valueOf(s1[i]);
        }
        double sum=0;
        int num1=0;
        int num2=0;
        for (int i=0;i<o.length;i++){
            if (o[i]>0){
                num1++;
            }else if (o[i]<0){
                num2++;
            }
            sum+=o[i];
        }
        System.out.println(num1);
        System.out.println(num2);
        int a=(int)sum;
        System.out.println(a);
        System.out.println(sum/(num1+num2));
    }
}

7-23 分队列

班级第一次列队,通常老师会让同学按身高排成一列,然后1、2报数,喊到1的同学向前一步,就这样,队伍就变成两列了。假设现在一个班级有n个同学,并且他们已经按身高排成了一列,同学按身高从1到n编号,你能告诉我最后哪些编号的同学站在了第一列么?

输入格式:
输入一个正整数n,表示班级的人数。

输出格式:
按顺序输出所有在第一列的同学对应的编号,每两个编号之间用一个空格隔开。

输入样例:

11

输出样例:

1 3 5 7 9 11


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        for (int i = 1; i <= N; i++) {
            if (i == 1){
                System.out.print(1);
            }else if(i % 2 != 0) {
                System.out.print(" " + i);
            }
        }
    }
}

7-24 累加器

请你实现一个累加器。输入n个非负整数,输出他们的和。 1<n<1000,而每个数则<10000。

输入格式:
输入包括两行。 第一行:包括一个整数n,表示总共有n个数。 第二行:包含n个整数。

输出格式:
输出n个数的和。

输入样例:

4
3 2 1 4

输出样例:

10


import java.util.Scanner;

public class Main {
     public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int sum = 0;
        for (int i = 0; i < N; i++) {
            int a = scanner.nextInt();
            sum += a;
        }
        System.out.println(sum);
    }
}

7-25 我是升旗手

一年一度的升旗手选拔又要到了,学校要求每个班级选出一位同学做升旗手的候选人。因 为升旗手对身高有严格的要求,所以班主任决定选班级里个子最高的同学(如果两位同学 一样高,则选任意一位)。你能很快地给老师答案么?

输入格式:
输入包括两行。 第一行:包括一个整数n,表示班级里共有n位同学。 第二行:包含n个三位数,表示每一位同学的身高。

输出格式:
输出身高最高的同学的身高。

输入样例:

4
130 125 129 140

输出样例:

140


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int max = 0;
        for (int i = 0; i < N; i++) {
            int a = scanner.nextInt();
            if (a > max){
                max = a ;
            }
        }
        System.out.println(max);
    }
}

7-26 兔子繁殖问题

已知有一对兔子,每个月可以生一对兔子,而小兔子一个月后又可以生一对小兔子(比如:2月份出生的小兔子4月份可以生育)。也就是说,兔子的对数为:第一个月1对,第二个月2对,第三个月3对,第四个月5对…假设兔子的生育期为两年,且不死。那么问题来了,你能说出每个月的兔子数么?

输入格式:
输入一个数n,表示第n个月,1<=n<=24。

输出格式:
输出这个月兔子的数目。

输入样例:

4

输出样例:

5


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(fun(n));
        scanner.close();
    }
    
    
    public static int fun(int n) {
        if (n == 0 || n == 1) {
            return 1;
        } else {
            return fun(n - 1) + fun(n - 2);
        }
    }
    
}

7-27 作品评分

全国中小学生Scratch作品大赛拉开了序幕。每个参赛选手可以通过网络直接上传作品。本次比赛人人可做评委。每个网络评委可以通过网络对每一件作品进行打分。评分系统也是请程序高手设计的,能自动去掉一个最高分和一个最低分,求出平均分。

输入格式:
输入数据包括两行: 第一行为n,表示n个评委,n>2。 第二行是n个评委的打分,分数之间有一个空格。打分是可以带有小数部分的。

输出格式:
输出平均分,结果保留两位小数。

输入样例:

6
10 9 8 7.9 9 9.5

输出样例:

8.88


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String N = scanner.nextLine();
        double max = 0;
        double min =11;
        double sum = 0;
        double sum1 = 0;
        if(N.equals("0")) {
            System.out.println("0");
        }
        String s = scanner.nextLine();
        String[] strings = s.split(" ");
        for(int i = 0; i < strings.length; i++) {
            if (max < Double.parseDouble(strings[i])) {
                max = Double.parseDouble(strings[i]);
              } else if (min > Double.parseDouble(strings[i])) {
                            min = Double.parseDouble(strings[i]);
                    }
                    sum += Double.parseDouble(strings[i]);
                }
        sum1 = (sum - max - min) / (Integer.parseInt(N) - 2);
        System.out.println(String.format("%.2f", sum1));
        }
    }
7-28 369寝室

369寝室是比较特殊的寝室,因为别的寝室都住了四个人,而369寝室只有三个人。也因为这个原因,寝室里的三位同学感情特别好。但是,毕业在即,三位小伙伴马上要分别。为了在未来的某个日子可以见面,三位小伙伴有了一个约定,假设在未来的某一年,三位小伙伴的年龄的末尾正好出现3、6、9三个数,那么他们会再次相聚。

现在问题来了,假设今年三位小伙伴的年龄分别是x,y,z,那么,他们三人最早几年后可以相聚呢?

输入格式:
输入数据包括三个整数x,y,z,分别表示三位伙伴的年龄。

输出格式:
如果小伙伴最早在n年后可以相见(不包括当前这一年),那么请输出这个n;如果100年内都不存在这样的情况,输出:so sad!

输入样例:

25 22 28

输出样例:

1


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean sad;
        boolean situation1 ,situation2 ,situation3 ;
        while (scanner.hasNextInt()) {
            sad = false;
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int z = scanner.nextInt();
        for(int i = 1; i <= 100; i++) {
            situation1 = false;
            situation2 = false;
            situation3 = false;
            if (i == 100) {
                    sad = true;
                }
            if ((x + i) % 10  == 3 || (y + i) % 10  == 3 || (z + i) % 10  == 3) {
                    situation1 = true;
                }
                if ((x + i) % 10  == 6 || (y + i) % 10  == 6 || (z + i) % 10  == 6) {
                    situation2 = true;
                }
                if ((x + i) % 10  == 9 || (y + i) % 10  == 9 || (z + i) % 10  == 9) {
                    situation3 = true;
                }
                if (situation1&&situation2&&situation3) {
                    System.out.println(i);
                    break;
                }
        }
        if(sad) {
            System.out.println("so sad!");
        }
    }
    }
}

7-29 等腰直角三角形

等腰直角三角形是指一个角是直角,且两条直角边相等的三角形。这里我们输出直角边长为n的等腰直角三角形的格式如下所示: 比如n=1,则输出:

*  

n=2,输出:

*
** 

n=3,输出:

*
** 
*** 

那么,你能用程序来实现么?

输入格式:
输入一个数n,表示三角形的边长。1<n<1000。

输出格式:
输出对应的用*表示的等腰直角三角形。

输入样例:

4

输出样例:

*
**
***
****

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        String s = "*";
        for (int i = 1; i <= N; i++) {
            System.out.println(s);
            s+="*";
        }
    }
}



7-30 画菱形

菱形是一种特殊的平行四边形,是四条边均相等的平行四边形。题目给出菱形的边长n,用*画出菱形。如n=1,输出:

*

n=2,输出:

 *
*** 
 * 

n=3,输出:

  * 
 ***
*****
 ***
  *

那么,你能用程序来实现么?

输入格式:
输入菱形的边长n,1<n<100。

输出格式:
输出对应的用*表示的菱形。

输入样例:

4

输出样例:

    *
   ***
  *****
 *******
  *****
   ***
    *

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for(int i = 0; i < n; i++) { 
            for(int k = n - i - 1 ; k > 0 ; k--) { 
                System.out.print(" ");
            }
            for (int j = 0;j <= 2 * i; j++) {  
                System.out.print("*");
            }
            System.out.println();
        }
        
        n -= 1;
        for (int i = n; i > 0; i--) { 
            for(int k = n - i +1; k > 0; k--) {
                System.out.print(" ");
            }

            for(int j = 0; j <= 2 * i - 2; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    
}

7-31 666

中国人非常喜欢6这个数字,因为大家总爱说66大顺啊。数学狂人李某人喜欢把什么都数字化,于是她把顺利这个词也定义了数量级,6代表1级顺利,66代表2级顺利,666代表3级顺利,以此类推。你看,数学狂人的世界总是让人无法理解。今天,李某人决定将数学进行到底,现在她设前n级顺利的和是sn。

sn=6+66+666+…+66…66(n个6)。

假设你已经知道了数字n,那么,你能帮李某人求出sn么?

输入格式:
输入一个正整数n,n的范围是[0,10)。

输出格式:
输出Sn的值。

输入样例:

2

输出样例:

72


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        long sn = 0;
        long a = 6;
        for (int i = 0; i < N; i++) {
            sn += a;
            a = a*10 +6;
        }
        System.out.println(sn);
    }
}

7-32 倒顺数字串

输入正整数n,输出它的倒顺数字串。如n=6时,输出

1 2 3 4 5 6 5 4 3 2 1

输入格式:
输入一个正整数n,n的范围是[1,50]。

输出格式:
n的倒顺数字串,每两个数字之间只用一个空格隔开。

输入样例:

6

输出样例:

1 2 3 4 5 6 5 4 3 2 1


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        for(int i = 1; i < N; i++) {
            System.out.print(i + " ");
        }
        for(int i = N; i >= 1; i--) {
            if(i == 1) {
                System.out.print(i);
            }else {
                System.out.print(i + " ");
            }
        }
    }
}

7-33 空心字母金字塔

输入一个大写的英文字母,输出空心的字母金字塔。

输入格式:
一个大写英文字母。

输出格式:
一个空心的大写英文字母金字塔,其中第1层的“A”在第1行的第40列,列从1开始计数。

输入样例:

E

输出样例:

                                       A
                                      B B
                                     C   C
                                    D     D
                                   EEEEEEEEE

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String N = scanner.nextLine();
        StringBuilder b = new StringBuilder("                                       "); 
        String c = " ";
        int k = 1;
        for (char i = 'A'; i < N.charAt(0); i++) {
            System.out.print(b);
            if (i == 'A'){
                System.out.println(i);
            }else {
                System.out.print(i);
                System.out.print(c);
                System.out.println(i);
                c += "  ";
            }
            b.delete(0,1);
            k++;
        }
        System.out.print(b);
        for (int i = 0; i < 2*k -1; i++) {
            System.out.print(N);
        }
    }
}

7-34 上三角数字三角形

输入一个正整数n,输出具有n层的上三角数字三角形。

输入格式:
只有一个正整数n,1<=n<=100。

输出格式:
一个上三角数字三角形,每个数字占四个字符位置。

输入样例:

5

输出样例:

   1   2   3   4   5
   6   7   8   9
  10  11  12
  13  14
  15

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = 1;
        int t = n;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < t; j++) {
                System.out.printf("%4d",k);
                k++;
            }
            t--;
            System.out.println();
        }
    }
}

7-35 又来一个上三角数字三角形

输入一个正整数n,输出具有n层的上三角数字三角形。

输入格式:
只有一个正整数n,1<=n<=100。

输出格式:
一个上三角数字三角形,每个数字占四个字符位置。

输入样例:

5

输出样例:

   1   6  10  13  15
   2   7  11  14
   3   8  12
   4   9
   5
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int k = 1;
        int t = a;
        int[][] map = new int[a][a];
        for (int i = 0; i < a; i++) {
            for (int j = 0; j < t; j++) {
                map[j][i] = k;
                k++;
            }
            t--;
        }
        for (int i = 0; i < a; i++) {
            for (int j = 0; j < a; j++) {
                if (map[i][j] != 0){
                    System.out.printf("%4s",map[i][j]);
                }
            }
            System.out.println();
        }
    }
}
7-36 组合找出最大值和最小值

从键盘输入0~9之内的4个整数,计算由这4个整数组合成的整数的最大值和最小值。若输入的不是整数则输出“number input error”若输入的整数不是0~9范围内则输出“Numerical range error”

输入格式:
从键盘输入0~9之内的4个整数,以空格分割

输出格式:
对每一组输入,在一行中输出两个整数,最大值和最小值。

输入样例:

1 5 9 3

输出样例:==

9531 1359

输入样例:

1 5 a 9

输出样例:

number input error

输入样例:

1 5 10 9

输出样例:

Numerical range error


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] strings = s.split(" ");
        int[] a = new int[4];
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].charAt(0) < '0' || strings[i].charAt(0) > '9'){
                System.out.println("number input error");
                return;
            }else if (strings[i].length() != 1){
                System.out.println("Numerical range error");
                return;
            }else {
                a[i] = Integer.parseInt(strings[i]);
            }
        }
        Arrays.sort(a);
        String s1 = "";
        for (int i = 3; i >= 0 ; i--) {
            s1 += a[i];
        }
        String s2 = "";
        for (int i = 0; i < 4; i++) {
            s2 += a[i];
        }
        System.out.println(Integer.parseInt(s1)+" "+Integer.parseInt(s2));
    }
}

7-37 古埃及探秘-金字塔

金字塔是继99乘法表决之后的一个经典图形排列题

题目要求:

要求用户可以自主控制塔身的层数, 完成如下金字体样式;

输入格式:
4

输出格式:

   *
  ***
 *****
*******

输入样例:
在这里给出一组输入。例如:

5
8

输出样例:
在这里给出相应的输出。例如:


    *
   ***
  *****
 *******
*********


       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        String s = "*";
        String b = " ";
        String[] strings = new String[N];
        for (int i = 0; i < N; i++) {
            String s1 = "";
            for (int j = 0; j < N - i -1; j++) {
                s1 += " ";
            }
            s1 += s;
            strings[i] = s1;
            System.out.println(s1);
            s+="**";
        }
    }
}
7-38 图片旋转

二维图片由一个个像素点组成,目前灰度图像使用一个0-255之间的整数表示某个像素点的像素值。编程完成图片旋转控制程序。

本题目要求读入2个整数m和n(<=20),作为图片的长宽尺寸。输入1个整数r,作为旋转角度(只能输入90 180 -90中的一个,90表示图形向左旋转90度,-90表示图形向右旋转90度)。然后按照行列输入图片像素值,

输入格式:
第一行:2个整数m和n(<=20)
第二行:2个整数r(只能是90 180 -90中的一个,否则提示:angle data error
第三行以后:输入m行n列的整数数据,必须在0-255之间,否则提示:matrix data error
以上输入的都是整数,若有非整数的数据输入,统一提示:data type error

输出格式:
按要求旋转后的图片矩阵数据

输入样例:
在这里给出一组输入。例如:

3 4
90
1 2 3 4
5 6 7 8
9 10 11 12

输出样例:
在这里给出相应的输出。例如:

4 8 12
3 7 11
2 6 10
1 5 9


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m,n;
        m = scanner.nextInt();
        n = scanner.nextInt();
        int op = scanner.nextInt();
        int[][] map = new int[m][n];
        if (op == 90 || op == -90 || op == 180){
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (!scanner.hasNextInt()){
                        System.out.println("data type error");
                        return;
                    }
                    map[i][j] = scanner.nextInt();
                    if (map[i][j] < 0 || map[i][j] > 255){
                        System.out.println("matrix data error");
                        return;
                    }
                }

            }
        }else {
            System.out.println("angle data error");
        }
        if (op == 90){
            for (int i = n-1; i >= 0; i--) {
                for (int j = 0; j < m; j++) {
                    if (j == m-1){
                        System.out.print(map[j][i]);
                    }else {
                        System.out.print(map[j][i] + " ");
                    }
                }
                System.out.println();
            }
        }else if (op == -90){
            for (int i = 0; i < n; i++) {
                for (int j = m-1; j >= 0; j--) {
                    if (j == 0){
                        System.out.print(map[j][i]);
                    }else {
                        System.out.print(map[j][i] + " ");
                    }
                }
                System.out.println();
            }
        }else if (op == 180){
            for (int i = m-1; i >= 0; i--) {
                for (int j = n-1; j >= 0; j--) {
                    if (j == 0){
                        System.out.print(map[i][j]);
                    }else {
                        System.out.print(map[i][j] + " ");
                    }
                }
                System.out.println();
            }
        }
    }
}

7-39 IP地址转换

一个IP地址是用四个字节(每个字节8个位)的二进制码组成。请将32位二进制码表示的IP地址转换为十进制格式表示的IP地址输出。

输入格式:
输入在一行中给出32位二进制字符串。

输出格式:
在一行中输出十进制格式的IP地址,其由4个十进制数组成(分别对应4个8位的二进制数),中间用“.”分隔开。

输入样例:
在这里给出一组输入。例如:

11001100100101000001010101110010

输出样例:
在这里给出相应的输出。例如:

204.148.21.114


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String s1 = s.substring(0,8);
        String s2 = s.substring(8,16);
        String s3 = s.substring(16,24);
        String s4 = s.substring(24,32);

        System.out.println(BinToTen(s1)+"."+BinToTen(s2)+"."+BinToTen(s3)+"."+BinToTen(s4));

    }

    private static int BinToTen(String s) {
        int sum = 0;
        for (int i = 7; i >= 0; i--) {
            if (s.charAt(i) == '1'){
            sum = (int) (sum + Math.pow(2,7-i));
            }
        }
        return sum;
    }

}

7-40 计算n位(3≤n≤7)水仙花数

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯特朗数(Armstrong number)。 水仙花数是指一个 n 位数(n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153;14+64+34+44=1634)。要求编写程序,计算n位(3≤n≤7)水仙花数。

输入格式:
输入在一行中给出一个正整数n(3≤n≤7)。

输出格式:
按递增顺序输出所有n位水仙花数,每个数字占一行。

输入样例:
在这里给出一组输入。例如:

3

输出样例:
在这里给出相应的输出。例如:

153
370
371
407

20
7-41 按正整数的相反数字顺序输出

本题目要求输入一个正整数,按数字的相反顺序输出。

输入格式:
输入一个正整数。

输出格式:
按输入正整数的数字相反顺序输出一个数字。

输入样例:

5236

输出样例:

6325


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
		int number,n=0; 
//		System.out.println("请输入一个整数:");
		Scanner scanner = new Scanner(System.in);
		number = scanner.nextInt();
//		System.out.println("逆向输出:");
		while(number != 0){ 
	    System.out.print(number%10); 
		number=number/10; 
		} 

	}
        
}

7-42 求质数

本题目要求输入一个大于等于2的正整数,求所有不大于该正整数的质数。

输入格式:
输入一个不小于2的正整数。

输出格式:
对输入的正整数,输出不大于该正整数的质数。

输入样例:

30

输出样例:

2 3 5 7 11 13 17 19 23 29


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int m ;
        for(int n = 2; n <= N; n++)
        {
	m = 2;
	while(n % m != 0)
	m++;
	if(m == n)
	System.out.print(n + " ");
        }
}

}

7-43 统计比指定元素大的个数-hebust

统计比指定元素大的个数

输入格式:
输入为2行,第一行为参照的整数元素,第二行为参与统计的整数元素,之间使用空格分割。

输出格式:
输出为比指定的参照元素大的元素个数。

输入样例:
在这里给出一组输入。例如:

5
2 3 6 7 3 4 9 8

输出样例:
在这里给出相应的输出。例如:

4


import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int max = scanner.nextInt();
        int count = 0 ;
        while (scanner.hasNextInt()){
            if (scanner.nextInt() > max){
                count++;
            }
        }
        System.out.println(count);
    }
}

7-44 倒顺字母字符串-hebust

根据输入n的大小(1=<n<=26),输出相对应的倒顺字符串。

输入格式:
输入格式:5

输出格式:
a b c d e d c b a

输入样例:
在这里给出一组输入。例如:

5

输出样例:
在这里给出相应的输出。例如:

a b c d e d c b a


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        if (n == 1){
            System.out.println('a');
            return;
        }
        for (int i = 0; i < n; i++) {
            System.out.print((char) ('a' + i) + " ");
        }
        for (int i = n-2; i >= 0; i--) {
            if (i == 0){
                System.out.print((char) ('a' + i));
            }else {
                System.out.print((char) ('a' + i) + " ");
            }
        }
    }
}

7-45 jmu-java-m02-循环求和

输入一组数,将其中的奇数、偶数分别求和并输出

输入格式:
输入n,然后输入n个整数

输出格式:
奇数和=x, 偶数和=y
其中x,y分别代表奇数和与偶数和。逗号(,)后面有一个空格。

输入样例:

10
1 2 1 1 1 2 1 2 2 2

输出样例:

奇数和=5, 偶数和=10


import java.util.Scanner;

public class Main {
     public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ou = 0;
        int ji = 0;
        for (int i = 0; i < n; i++) {
            int a = scanner.nextInt();
            if (a % 2 == 0){
                ou += a;
            }else {
                ji +=a;
            }
        }
        System.out.println("奇数和="+ji + ", "+"偶数和="+ou);
    }
}

7-46 jmu-java-m02-不定循环拼接符合条件的字符串

输入一组字符串,直到碰到end为之。将其中字符串长度是3的倍数的字符串使用空格拼接起来,并输出

输入格式:
输入一组以空格或者回车换行分隔的字符串,最后一个字符串为end。

输出格式:
将输入字符串中长度是3的倍数的字符串使用空格拼接起来。
注意:行尾有一个空格。

输入样例:

a abc 123456 22 45jd 123456789 end

输出样例:

abc 123456 123456789

import java.util.Scanner;

public class Main {
     public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        String t = "";
        while (scanner.hasNext()){
            String s = scanner.next();
            if (s.equals("end")){
                break;
            }
            if (s.length() % 3 == 0){
                t+=s;
                t += " ";
            }
        }
        System.out.println(t);
    }
}

未完待续…

  • 129
    点赞
  • 580
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值