R7-1 求最大值

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

输入格式:

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

输出格式:

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

输入样例:

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

18 -299

输出样例:

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

18
import java.util.*;

public class Main {
    
    public static void main(String[] agrs) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        if (a < b) {
            int t = a;
            a = b;
            b = t;
        }
        System.out.println(a);
    }
    
}

 

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

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

输入格式:

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

输出格式:

输出两个数的和

输入样例:

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

输出样例:

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

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

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
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int foot ,inch;
		int memter = cin.nextInt();
		foot = (int) (memter * 12 / 30.48);
		System.out.println((foot / 12) + " " + (foot % 12));
	}

}

 

R7-16 二进制的前导的零

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

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

输入格式:

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

输出格式:

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

输入样例:

256

输出样例:

23
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        String ans = Integer.toBinaryString(x);
        if(x!=0)
            System.out.println(32-ans.length());
        else 
            System.out.println(32);
    }
}

 

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

分数 10

全屏浏览题目

切换布局

作者 老象

单位 贵州师范学院

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

输入格式:

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

输出格式:

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

输入样例:

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

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

输出样例:

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

3 4
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = cin.nextInt();
        }
        int maxcnt = -1 ,x = a[0];
        for (int i = 0; i < n; i++) {
            int cnt = 0;
            for (int j = 0; j < n; j++) {
                if (a[i] == a[j]) {
                    cnt++;
                }
            }
            if (cnt > maxcnt) {
                maxcnt = cnt;
                x = a[i];
            }
        }
        System.out.println(x + " " + maxcnt);
    }
    
}

 

R7-28 空心字母金字塔

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

输入格式:

一个大写英文字母。

输出格式:

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

输入样例:

E

输出样例:

A
                                      B B
                                     C   C
                                    D     D
                                   EEEEEEEEE

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

以前的方法

import java.util.*;
 
public class Main {
 
	public static void main (String[] args) {
        Scanner cin = new Scanner(System.in);
        char c = cin.next().charAt(0);
//         System.out.println(c);
        int x = c -'A' + 1;
//         System.out.println(x);
//                                        A
//                                       B B
//                                      C   C
//                                     D     D
//                                    EEEEEEEEE
        int cnt = 40;
        char ch = 'A';
        int num = 0;
//         System.out.println(ch);
        int m = x;
        x = x * 2 + 1;
        for (int  i = x / 2 + 1; i >=3 ; i--) {
            for (int j = 1; j < cnt; j ++) System.out.print(" ");
            for (int j = 1; j <= x; j ++) {
                if (i == j || j == x + 1 -i) 
                    System.out.print(ch);
                else if(j > i && j <= x + 1 - i){
                    System.out.print(" ");
                }
            }
            ch++;
            cnt--;
            System.out.println();
        }
        cnt-=2;
        for (int i = 0; i <= cnt; i++) System.out.print(" ");
        for (int i = 0; i <= 2 * m - 2; i++) System.out.print(ch);
    }
}

更新了之后的方法

import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner cin = new Scanner(System.in);
        char c = cin.next().charAt(0);
        int n = c - 'A' + 1;
        char ch = 'A';
        int block = 40;
        int num = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < block; j++) System.out.print(" ");
            if (i == n) {
                for (int j = 0; j < i * 2 - 1; j++) System.out.print(ch);
                System.exit(0);
            }
            System.out.print(ch);
            if (i > 1) {
                for (int j = 0; j < num; j++) System.out.print(" ");
                num += 2;
                System.out.print(ch);
            }
            ch++;
            block--;
            System.out.println();
        }
        
    }
    
}

R7-29 上三角数字三角形

输入一个正整数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.*;

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

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

输入一个正整数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.*;

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

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

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

输入格式:

输入一个正整数。

输出格式:

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

输入样例:

5236

输出样例:

6325
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner cin = new Scanner(System.in);
        long  x = cin.nextLong();
        while (x != 0) {
            System.out.print(x % 10);
            x /= 10;
        }
        
    }
    
}

R7-39 计算工资数

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

输入格式:

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

输出格式:

输出这个员工的工资数

输入样例:

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

40

输出样例:

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

800
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner cin = new Scanner(System.in);
        int h = cin.nextInt();
        if (h <= 120 && h > 0) {
            System.out.println(h * 20);
        }
        else if (h > 120){
            System.out.println((120 * 20 + (h - 120) * 40));
        }
        else {
            System.out.println(0);
        }
    }
    
}

R7-35 小明走格子

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

java面向对象程序设计题 java面向对象程序设计试题_java面向对象程序设计题

输入格式:

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

输出格式:

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

输入样例:

2
5
3

输出样例:

15
4
import java.util.*;
import java.io.*;
public class Main {
    
    public static void main(String[] args) throws IOException{
        
        StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        cin.nextToken();
        int n = (int)cin.nval;
        int[] a = new int[10010];
        int[] b = new int[110];
        int maxn = 0;
        for (int i = 0; i < n; i++) {
            cin.nextToken();
            a[i] = (int)cin.nval;
            maxn = Math.max(maxn ,a[i]);
        }
        b[0] = 1;
        b[1] = 1;
        b[2] = 2;
        b[3] = 4;
        b[4] = 8;
        for (int i = 5; i <= maxn; i++) {
            b[i] = 2 * b[i - 1] - b[i - 5];
        }
        for (int i = 0; i < n; i++) {
            System.out.println(b[a[i]]);
        }
    }
    
}