• Java面向对象程序设计综合练习1(编程题)


    R7-1 求最大值

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

    输入格式:

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

    输出格式:

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

    输入样例:

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

    18 -299
    

    输出样例:

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

    18
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] agrs) {
    4. Scanner cin = new Scanner(System.in);
    5. int a = cin.nextInt();
    6. int b = cin.nextInt();
    7. if (a < b) {
    8. int t = a;
    9. a = b;
    10. b = t;
    11. }
    12. System.out.println(a);
    13. }
    14. }

     

    R7-2 jmu-java-01入门-基本输入

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

    输入格式:

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

    输出格式:

    输出两个数的和

    输入样例:

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

    输出样例:

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

    1. 2
    2. 5
    3. 0
    4. -200
    5. 10
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. while (true){
    6. int a = sc.nextInt();
    7. int b = sc.nextInt();
    8. System.out.println(a+b);
    9. }
    10. }
    11. }

    R7-7 cm2inch

    Given foot and inch, the meter is (foot+inch/12)×0.3048.

    What sould be the feet and inches for an input centimetre?

    PS:One foot is 12 inches.

    Input Format:

    One integer in terms of cm.

    Output Format:

    One integer for the feet and another integer for the inches.

    Sample Input:

    170
    

    Sample Output:

    5 6
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. int foot ,inch;
    6. int memter = cin.nextInt();
    7. foot = (int) (memter * 12 / 30.48);
    8. System.out.println((foot / 12) + " " + (foot % 12));
    9. }
    10. }

     

    R7-16 二进制的前导的零

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

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

    输入格式:

    一个整数,在32位的整数可以表达的范围内。

    输出格式:

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

    输入样例:

    256
    

    输出样例:

    23
    1. import java.util.*;
    2. public class Main{
    3. public static void main(String[] args) {
    4. Scanner input = new Scanner(System.in);
    5. int x = input.nextInt();
    6. String ans = Integer.toBinaryString(x);
    7. if(x!=0)
    8. System.out.println(32-ans.length());
    9. else
    10. System.out.println(32);
    11. }
    12. }

     

    R7-17 JAVA-求整数序列中出现次数最多的数

    分数 10

    全屏浏览题目

    切换布局

    作者 老象

    单位 贵州师范学院

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

    输入格式:

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

    输出格式:

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

    输入样例:

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

    1. 10
    2. 3
    3. 2
    4. -1
    5. 5
    6. 3
    7. 4
    8. 3
    9. 0
    10. 3
    11. 2

    输出样例:

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

    3 4
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. int n = cin.nextInt();
    6. int[] a = new int[n];
    7. for (int i = 0; i < n; i++) {
    8. a[i] = cin.nextInt();
    9. }
    10. int maxcnt = -1 ,x = a[0];
    11. for (int i = 0; i < n; i++) {
    12. int cnt = 0;
    13. for (int j = 0; j < n; j++) {
    14. if (a[i] == a[j]) {
    15. cnt++;
    16. }
    17. }
    18. if (cnt > maxcnt) {
    19. maxcnt = cnt;
    20. x = a[i];
    21. }
    22. }
    23. System.out.println(x + " " + maxcnt);
    24. }
    25. }

     

    R7-28 空心字母金字塔

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

    输入格式:

    一个大写英文字母。

    输出格式:

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

    输入样例:

    E
    

    输出样例:

    1. A
    2. B B
    3. C C
    4. D D
    5. EEEEEEEEE

    刚开始用的方法是从空心菱形上强行拿过来的太久没写又忘了T_T后面干脆直接想了一个暴力做法

    以前的方法

    1. import java.util.*;
    2. public class Main {
    3. public static void main (String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. char c = cin.next().charAt(0);
    6. // System.out.println(c);
    7. int x = c -'A' + 1;
    8. // System.out.println(x);
    9. // A
    10. // B B
    11. // C C
    12. // D D
    13. // EEEEEEEEE
    14. int cnt = 40;
    15. char ch = 'A';
    16. int num = 0;
    17. // System.out.println(ch);
    18. int m = x;
    19. x = x * 2 + 1;
    20. for (int i = x / 2 + 1; i >=3 ; i--) {
    21. for (int j = 1; j < cnt; j ++) System.out.print(" ");
    22. for (int j = 1; j <= x; j ++) {
    23. if (i == j || j == x + 1 -i)
    24. System.out.print(ch);
    25. else if(j > i && j <= x + 1 - i){
    26. System.out.print(" ");
    27. }
    28. }
    29. ch++;
    30. cnt--;
    31. System.out.println();
    32. }
    33. cnt-=2;
    34. for (int i = 0; i <= cnt; i++) System.out.print(" ");
    35. for (int i = 0; i <= 2 * m - 2; i++) System.out.print(ch);
    36. }
    37. }

    更新了之后的方法

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. char c = cin.next().charAt(0);
    6. int n = c - 'A' + 1;
    7. char ch = 'A';
    8. int block = 40;
    9. int num = 1;
    10. for (int i = 1; i <= n; i++) {
    11. for (int j = 1; j < block; j++) System.out.print(" ");
    12. if (i == n) {
    13. for (int j = 0; j < i * 2 - 1; j++) System.out.print(ch);
    14. System.exit(0);
    15. }
    16. System.out.print(ch);
    17. if (i > 1) {
    18. for (int j = 0; j < num; j++) System.out.print(" ");
    19. num += 2;
    20. System.out.print(ch);
    21. }
    22. ch++;
    23. block--;
    24. System.out.println();
    25. }
    26. }
    27. }

     

    R7-29 上三角数字三角形

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

    输入格式:

    只有一个正整数n,1<=n<=100。

    输出格式:

    一个上三角数字三角形,每个数字占四个字符位置。

    输入样例:

    5
    

    输出样例:

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

    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. int n = cin.nextInt();
    6. int x = 1;
    7. for (int i = n; i > 0; i--) {
    8. for (int j = 0; j < i; j++) {
    9. System.out.printf("%4d",x);
    10. x++;
    11. }
    12. System.out.println();
    13. }
    14. }
    15. }

    R7-30 又来一个上三角数字三角形

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

    输入格式:

    只有一个正整数n,1<=n<=100。

    输出格式:

    一个上三角数字三角形,每个数字占四个字符位置。

    输入样例:

    5
    

    输出样例:

    1. 1 6 10 13 15
    2. 2 7 11 14
    3. 3 8 12
    4. 4 9
    5. 5
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. int n = cin.nextInt();
    6. int[][] a = new int[n+1][n+1];
    7. int x = 1;
    8. int m = n;
    9. for (int i = 1; i <= n; i++) {
    10. for (int j = 1; j <= m; j++) {
    11. a[j][i] = x++;
    12. }
    13. m--;
    14. }
    15. m = n;
    16. for (int i = 1; i <= n; i++) {
    17. for (int j = 1; j <= m; j++) {
    18. System.out.printf("%4d",a[i][j]);
    19. }
    20. System.out.println();
    21. m--;
    22. }
    23. }
    24. }

    R7-31 按正整数的相反数字顺序输出

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

    输入格式:

    输入一个正整数。

    输出格式:

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

    输入样例:

    5236
    

    输出样例:

    6325
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. long x = cin.nextLong();
    6. while (x != 0) {
    7. System.out.print(x % 10);
    8. x /= 10;
    9. }
    10. }
    11. }

    R7-39 计算工资数

    某公司标准上班时间是120小时,每小时工钱是20元, 如果上班时间超出了120小时,超出部分每小时按2倍工资发放。请编写程序计算员工月工资。

    输入格式:

    输入一个员工的工作小时数

    输出格式:

    输出这个员工的工资数

    输入样例:

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

    40
    

    输出样例:

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

    800
    1. import java.util.*;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner cin = new Scanner(System.in);
    5. int h = cin.nextInt();
    6. if (h <= 120 && h > 0) {
    7. System.out.println(h * 20);
    8. }
    9. else if (h > 120){
    10. System.out.println((120 * 20 + (h - 120) * 40));
    11. }
    12. else {
    13. System.out.println(0);
    14. }
    15. }
    16. }

    R7-35 小明走格子

    从A点到B点有n个格子,小明现在要从A点到B点,小明吃了些东西,补充了一下体力,他可以一步迈一个格子,也可以一步迈两个格子,也可以一步迈3个格子,也可以一步迈4个格子。请编写程序计算小明从A点到B点一共有多少种走法。

    输入格式:

    输入包含多组数据,第一行为一个整数m,m不超过10000,表示输入数据组数。接下来m行,每行为一个整数n(保证对应的输出结果小于231),表示从A点到B点的格子数。

    输出格式:

    输出为m个整数,表示对于每组数据小明从A点到B点的走法数。

    输入样例:

    1. 2
    2. 5
    3. 3

    输出样例:

    1. 15
    2. 4

    1. import java.util.*;
    2. import java.io.*;
    3. public class Main {
    4. public static void main(String[] args) throws IOException{
    5. StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    6. cin.nextToken();
    7. int n = (int)cin.nval;
    8. int[] a = new int[10010];
    9. int[] b = new int[110];
    10. int maxn = 0;
    11. for (int i = 0; i < n; i++) {
    12. cin.nextToken();
    13. a[i] = (int)cin.nval;
    14. maxn = Math.max(maxn ,a[i]);
    15. }
    16. b[0] = 1;
    17. b[1] = 1;
    18. b[2] = 2;
    19. b[3] = 4;
    20. b[4] = 8;
    21. for (int i = 5; i <= maxn; i++) {
    22. b[i] = 2 * b[i - 1] - b[i - 5];
    23. }
    24. for (int i = 0; i < n; i++) {
    25. System.out.println(b[a[i]]);
    26. }
    27. }
    28. }

  • 相关阅读:
    Janus介绍
    快速文本分类(FastText)
    AutoSAR配置与实践(深入篇)8.1 BSW的WatchDog功能(上)
    8.3 矢量图层点要素单一符号使用二
    【树莓派不吃灰系列】快速导航
    LeNet学习CIFAR-10数据集识别图片类别
    抖音小店无货源,正处于红利期内的电商项目,新手能操作吗?
    leetcode321场周赛python
    协议-TCP协议-基础概念02-TCP握手被拒绝-内核参数-指数退避原则-TCP窗口-TCP重传
    【conda】——pack打包32位python,在服务器报 no such file
  • 原文地址:https://blog.csdn.net/qq_51916951/article/details/125418994