记录一下自己的洛谷的题解

不出意外应该都是很基础的练习题,最近在学java,刷洛谷的简单题,熟悉熟悉语法

P5727
 1 /*
 2  * 给出一个正整数 n (n<=100),然后对这个数字一直进行下面的操作:
 3  * 如果这个数字是奇数,那么将其乘 3 再加 1,否则除以 2。
 4  * 经过若干次循环后,最终都会回到 1。经过验证很大的数字(7e11)
 5  * 都可以按照这样的方式比变成 1,所以被称为“冰雹猜想”。
 6  * 例如当 n 是 20,变化的过程是 [20, 10, 5, 16, 8, 4, 2, 1]。
 7  *
 8  * 根据给定的数字,验证这个猜想,并从最后的 1 开始,倒序输出整个变化序列。
 9  * */
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Scanner;
13 
14 public class Main {
15     public static void main(String[] args) {
16         Scanner sc = new Scanner(System.in);
17         int n = sc.nextInt();
18         List<Integer> list = new ArrayList<>();
19         while (n > 1) {
20             list.add(n);
21             if (n % 2 == 1) {
22                 n = n * 3 + 1;
23             } else {
24                 n = n / 2;
25             }
26         }
27         list.add(1);
28     
29         for (int i = list.size()-1; i >=0; i--) {
30             System.out.print(list.get(i)+" ");
31         }
32     }
33 }

P5719

 1 /*
 2  * 给定 n(n≤10000) 和 k(k≤100),
 3  * 将从 1 到 n 之间的所有正整数可以分为两类:
 4  * A 类数可以被 k 整除(也就是说是 k 的倍数),
 5  * 而 B 类数不能。请输出这两类数的平均数,
 6  * 精确到小数点后 1 位,用空格隔开。
 7  * 数据保证两类数的个数都不会是 0。
 8  * */
 9 
10 import java.util.Scanner;
11 
12 public class Main {
13     public static void main(String[] args) {
14         Scanner sc = new Scanner(System.in);
15         int n = sc.nextInt(),
16             k = sc.nextInt();
17         
18         double sumA = 0, sumB = 0,
19             countA = 0, countB = 0;
20         
21         for (int i = 1; i <= n; i++) {
22             if (i % k == 0) {
23                 sumA += i;
24                 countA++;
25             } else {
26                 sumB += i;
27                 countB++;
28             }
29         }
30         
31         System.out.printf("%.1f %.1f",sumA / countA , sumB / countB);
32     }
33 }

P5717

 1 /*
 2  * 给出三条线段 a,b,c 的长度,均是不大于 10000 的整数。
 3  * 打算把这三条线段拼成一个三角形,它可以是什么三角形呢?
 4  *
 5  * 如果三条线段不能组成一个三角形,输出Not triangle;
 6  * 如果是直角三角形,输出Right triangle;
 7  * 如果是锐角三角形,输出Acute triangle;
 8  * 如果是钝角三角形,输出Obtuse triangle;
 9  * 如果是等腰三角形,输出Isosceles triangle;
10  * 如果是等边三角形,输出Equilateral triangle。
11  * 如果这个三角形符合以上多个条件,请分别输出,并用换行符隔开。
12  *
13  * a+b>c构成三角形
14  *
15  * 当两短边的平方和大于一长边的平方,说明是锐角三角形。
16  * 当两短边的平方和等于一长边的平方,说明是直角三角形。
17  * 当两短边的平方和小于一长边的平方,说明是钝角三角形。
18  * */
19 
20 import java.util.Arrays;
21 import java.util.Scanner;
22 
23 public class Main {
24     public static void main(String[] args) {
25         Scanner sc = new Scanner(System.in);
26         int[] arr = new int[3];
27         for (int i = 0; i < arr.length; i++) {
28             arr[i] = sc.nextInt();
29         }
30         Arrays.sort(arr); // 升序 (0<=1<=2)
31         
32         // 三角形判断
33         if (arr[0] + arr[1] <= arr[2]) {
34             System.out.println("Not triangle");
35             return;
36         }
37         // 角类型判断
38         int a = arr[0]*arr[0]+arr[1] * arr[1]; // 两短边平方和
39         int b = arr[2]*arr[2]; // 长边的平方
40         if (a>b) {
41             System.out.println("Acute triangle");
42         }else if (a < b) {
43             System.out.println("Obtuse triangle");
44         }else {
45             System.out.println("Right triangle");
46         }
47         // 等腰判断 (由于数组已排序,所以条件可以适当省去)
48         if (arr[0] == arr[1] || arr[1] == arr[2]) {
49             System.out.println("Isosceles triangle");
50             if (arr[0] == arr[2]) {
51                 System.out.println("Equilateral triangle");
52             }
53         }
54     }
55 }

P5716

 1 /*
 2  * 输入年份和月份,输出这一年的这一月有多少天。需要考虑闰年。
 3  * */
 4 
 5 import java.io.BufferedReader;
 6 import java.io.InputStreamReader;
 7 import java.util.StringTokenizer;
 8 
 9 public class Main {
10     public static void main(String[] args) throws Exception {
11         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12         StringTokenizer st = new StringTokenizer(br.readLine());
13         
14         int year = Integer.parseInt(st.nextToken()),
15             month = Integer.parseInt(st.nextToken()),
16             days = 0;
17         //是否是闰年
18         boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0))
19                              || (year % 400 == 0);
20         switch (month) {
21             case 1:
22             case 3:
23             case 5:
24             case 7:
25             case 8:
26             case 10:
27             case 12:
28                 days = 31;
29                 break;
30             case 2:
31                 days = 28;
32                 break;
33             default:
34                 days = 30;
35                 break;
36         }
37         if (isLeapYear && month == 2) {
38             days++;
39         }
40         
41         System.out.println(days);
42     }
43 }

P1914

 1 /*
 2  * 密码是由一个字符串组成。
 3  * 密码是由原文字符串(由不超过 50 个小写字母组成)中
 4  * 每个字母向后移动 n 位形成的。
 5  * z 的下一个字母是 a,如此循环。
 6  * 现在找到了移动前的原文字符串及 n,请你求出密码。
 7  * */
 8 
 9 import java.util.Scanner;
10 
11 public class Main {
12     public static void main(String[] args) {
13         Scanner sc = new Scanner(System.in);
14         int n = sc.nextInt();
15         String oldStr = sc.next();
16         char[] oldChars = oldStr.toCharArray();
17         for (int i = 0; i < oldChars.length; i++) {
18             oldChars[i] += n % 26;
19             if (oldChars[i] > 'z') {
20                 oldChars[i] = (char) (oldChars[i] - 'z' - 1 + 'a');
21             }
22         }
23     
24         for (char oldChar : oldChars) {
25             System.out.print(oldChar);
26         }
27     }
28 }

P1909

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         int n = sc.nextInt(); // 需要的铅笔数量
 7         int[][] arr = new int[3][2];
 8         for (int i = 0; i < arr.length; i++) {
 9             for (int j = 0; j < arr[i].length; j++) {
10                 arr[i][j] = sc.nextInt();
11             }
12         }
13         
14         int min;
15         
16         if (n % arr[0][0] != 0) {
17             min = arr[0][1] * (n / arr[0][0] + 1);
18         } else {
19             min = arr[0][1] * (n / arr[0][0]);
20         }
21         
22         if (n % arr[1][0] != 0) {
23             min = Math.min(min, arr[1][1] * (n / arr[1][0] + 1));
24         } else {
25             min = Math.min(min, arr[1][1] * (n / arr[1][0]));
26         }
27         if (n % arr[2][0] != 0) {
28             min = Math.min(min, arr[2][1] * (n / arr[2][0] + 1));
29         } else {
30             min = Math.min(min, arr[2][1] * (n / arr[2][0]));
31         }
32         
33         System.out.println(min);
34     }
35 }

P1085

 1 /*
 2  * 津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。
 3  * 假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。
 4  * 请你帮忙检查一下津津下周的日程安排,
 5  * 看看下周她会不会不高兴;如果会的话,哪天最不高兴。
 6  *
 7  * 输入格式
 8  * 输入包括7行数据,分别表示周一到周日的日程安排。
 9  * 每行包括两个小于10的非负整数,用空格隔开,
10  * 分别表示津津在学校上课的时间和妈妈安排她上课的时间。
11  *
12  * 输出格式
13  * 一个数字。如果不会不高兴则输出00,如果会则输出最不高兴的是周几(用1, 2, 3, 4, 5, 6, 7
14  * 分别表示周一,周二,周三,周四,周五,周六,周日)。
15  * 如果有两天或两天以上不高兴的程度相当,则输出时间最靠前的一天。
16  * */
17 
18 
19 import java.util.Scanner;
20 
21 public class Main {
22     public static void main(String[] args) {
23         Scanner sc = new Scanner(System.in);
24         int[] a = new int[7];
25         int[] b = new int[7];
26         int[] notHappy = new int[7];
27         for (int i = 0; i < notHappy.length; i++) {
28             a[i] = sc.nextInt();
29             b[i] = sc.nextInt();
30             notHappy[i] = a[i] + b[i] - 8;
31         }
32         int maxIndex = 0;
33         for (int i = 1; i < notHappy.length; i++) {
34             maxIndex = notHappy[i] > notHappy[maxIndex] ? i : maxIndex;
35         }
36         System.out.println(maxIndex+1);
37     }
38 }

P1055

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         String s = sc.nextLine();
 7         String str= s.replace("-", "");
 8         int sum = 0;
 9         for (int i = 0; i < str.length()-1; i++) {
10             int a = Character.getNumericValue(str.charAt(i));
11             sum+=a*(i+1);
12         }
13         int code = sum%11;
14         String s1 = str.substring(str.length() - 1);
15         String s2;
16         if (code == 10) {
17             s2="X";
18         }else {
19             s2 = String.valueOf(code);
20         }
21         if (s.substring(s.length() - 1).equals(s2)) {
22             System.out.println("Right");
23         }else {
24             System.out.println(s.substring(0,s.length() - 1)+s2);
25         }
26         
27     }
28 }
原文地址:https://www.cnblogs.com/yao-xi/p/13748637.html