【沈师PTA】JAVA程序设计-第2章习题集

一、判断题

T )语句 System.out.println(4+8+“Hello,world!”); 的输出结果是12Hello,world!。
T )Java的字符类型采用的是Unicode编码,每个Unicode码占16个比特。
T )boolean done = true; is a valid assignment statement.
F )The following answers is the correct way to declare a Boolean variable named truth: boolean truth == true;
T )Java的各种数据类型占用固定长度,与具体的软硬件平台环境无关。
T )boolean型数据的值只有true和false。
F )char of Java is 8-bit.
F )[ ] use to delineate a block of statements in Java.
F )Java的字符类型采用的是ASCII编码。

二、单选题

  1. What will be printed out if you attempt to compile and run the following code ( C )。
int i=1; 
switch (i) { 
	case 0:System.out.println("zero");break; 
	case 1:System.out.println("one"); 
	case 2:System.out.println("two"); 
	default:System.out.println("default"); 
}

A. one
B. one, default
C. one, two, default
D. default
2. For the code below: What is the output( B )。

boolean m = true;
if ( m=false )
System.out.println(“False”);
else
System.out.println(“True”);

A. False
B. True
C. None
D. An error will occur when running
3. 下面的方法,当输入为2的时候返回值是多少( D )。

public int getValue(int i) {
         int result = 0;
         switch (i) {
            case 1:
                result = result + i;
            case 2:
                result = result + i * 2;
            case 3:
                result = result + i * 3;
        }
        return result;
 }

A. 0
B. 2
C. 4
D. 10
4. 假设有如下程序:最终执行结果是什么( C )。

public class Demo {
      public static void main(String args[]) {
             int sum = 0 ;
            int x = 10 ;
            while (x > 0) {
                sum += x ;
                        }
            System.out.println(sum) ;
       }
}

A. 55.0
B. 10.0
C. 程序错误,死循环
D. 55.0
5. For code below: After executing line 2, where will the program jump( D )。

int i, j;
Loop1:                       //    1
for ( i=0; i<20; i++ ) {
    for ( j=0; j<i*i; j++ ) {
        if ( j*2 ==i )
            break Loop1; }   //    2
    i=4;                     //    3
}
i=5;                         //    4

A. 1
B. 2
C. 3
D. 4
6. 分析下列代码的运行结果是什么( A )。

void looper(){
    int x=0;
    one:
    while(x<20) {
        two:
            System.out.print(++x);
            if(x>3)
              break two;
    }
}

A. 编译错误
B. 0
C. 1
D. 2
7. 在Java中,以下程序段的输出结果是( B )。

int n=9;
while(n>6){
   n--;
   System.out.print(n); 
}

A. 987
B. 876
C. 8765
D. 9876
8. 整型数据类型中,需要内存空间最少的是( D )。
A. short
B. long
C. int
D. byte
9. 当编译并运行下列程序段时,运行结果是什么( C )。

public class Test {
    public static void main(String[ ] args) {
        int i=0;
        while (i--<0){
             System.out.println("The value of i is "+i);
         }
         System.out.println("The end");
     }
}

A. 编译时错误
B. 运行时错误
C. The end
D. The value of i is 0
10. What will happen when you attempt to compile and run the following code( D )。

int Output=10;
boolean b1 = false;
if((b1==true) && ((Output+=10)==20)){
    System.out.println("We are equal "+Output);
} else {
    System.out.println("Not equal! "+Output);
}

A. Compile error, attempting to preform binary comparison on logical data type
B. Compilation and output of “We are equal 10”
C. Compilation and output of “Not equal! 20”
D. Compilation and output of "Not equal! 10"
11. 设有变量定义: short a = 300; 则以下哪一条语句会导致编译错误( B )。
A. a += 3;
B. a = (short)a + 3;
C. a = (byte)(a + 3);
D. a = (short)(a * 100);
12. 下面哪单词是Java语言的关键字( B )。
A. Float
B. this
C. string
D. unsigned
13.当编译运行下列代码时,运行结果是什么( D )。

public class Demo{
  public static void main(String args[]){
    int i=012; int j=034;
    int k=056;int l=078;
    System.out.println(i);
    System.out.println(j);
    System.out.println(k); }
}

A. 输出12,34和56
B. 输出24,68和112
C. 输出10,28和46
D. 编译错误
14. MAX_LENGTH是int型public成员变量,变量值保持为常量55,用简短语句定义这个变量( D )。
A. public int MAX_LENGTH=55
B. final int MAX_LENGTH=55
C. final public int MAX_LENGTH=55
D. public final int MAX_LENGTH=55
15. 以下选项中没有语法错误的是( C )。
A. while (int i<7) {i++;System.out.println(“i is “+i);}
B. int j=3; while(j) {System.out.println(“ j is “+j);}
C. int j=0;for(int k=0; j + k !=10; j++,k++) {System.out.println(“ j is “+ j + “k is”+ k);}
D. int j=0;do{System.out.println( “j is “+j++);if (j == 3) {continue loop;}}while (j<10);
16. 下列不可作为java语言标识符的是( D )。
A. a2
B. $2
C. _2
D. 22
17. 下面代码运行结果显示( C )。

double temperature = 50;
if (temperature >= 100)
   System.out.println("too hot");
else if (temperature <= 40)
   System.out.println("too cold");
else
   System.out.println("just right");

A. too hot
B. too cold
C. just right
D. too hot too cold just right
18. 下面代码将输出( B )行 “Welcome to Java”?。

int count = 0;
do {System.out.println("Welcome to Java");
} while (count++ < 10);

A. 10
B. 11
C. 9
D. 1
19. 在JAVA中,给定代码片段如下所示,则编译运行后,输出结果是( B )。

for (int i = 0; i < 10; i++) {
    if (i == 10 - i) {
        break;
    }
    if (i % 3 != 0) {
        continue;
    }
    System.out.print(i + " ");
}

A. 0
B. 0 3
C. 0 3 6
D. 0 3 6 9
20. 分析下面这段Java代码,它的运行结果是( C )。

Import java.io.*;
Public class B{
    Public static void main(string [] args){
        int i=12;
        System.out.println(i+=i-=i*=i);
      }
}

A. 100
B. 0
C. -120
D. 程序无法编译
21. 假设有如下程序:最终的执行结果是什么( A )?

public class Demo {
      public static void main(String args[]) {
            String str = "" ;
            for (int x = 0 ; x < 5 ; x ++) {
                   str += x ;
            }
          System.out.println(str) ;
      }
}

A. 01234
B. 10.0
C. 14.0
D. 25.0
22. Which statement below is incorrect: ( A )。
A. float a = 2.0
B. double b=2.0
C. int c=2
D. long d=2
23. 以下程序段的输出结果是( C )。

class Test {
     public static void main(String[] args) {
        System.out.println(4 + 5 + "" + 3 + 6);
    }
}

A. 99
B. 4536
C. 936
D. 459
24. 下列选项中不属于本段代码输出结果的是( D )。

public class Main{
    public static void main(String args[]) {
      one:
      two:
      for(int i=0; i<3; i++) {
          three:
          for(int j=10; j<30; j+=10) {
             System.out.println(i+j);
             if(i>0)
                break one;  }
       }
    }
}

A.10
B.20
C.11
D.21
25. 有以下方法的定义,请选择该方法的返回类型( A )。

ReturnType  method(byte x, double y)  {
    return  (short)x/y*2;
}

A. double
B. byte
C. short
D. int
26. Which variable definition below is valid in Java( A )?
A. boolean b;
B. extern int i;
C. unsigned int k;
D. int i[10];
27. Which is the value of temp after the code’s execution( B )?

long temp = (int)3.9; 
temp %= 2;

A. 0
B. 1
C. 2
D. 3
28. Which line below will not generate warning or error when compiling( D )。
A. float f = 1.3;
B. char c = “a”;
C. byte b = 257;
D. int i = 10;
29. 假设有如下程序:最终程序的执行结果是什么( D )?

public class Demo {
      public static void main(String args[]) {
            long num = 100 ;
            int x = num + 2 ;
            System.out.println(x) ;
     }
}

A. 102.0
B. 1002.0
C. 100.0
D. 程序错误
30. 循环执行后,count的值是( B

int count = 0;
do { System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);

A. 11
B. 10
C. 9
D. 8
31. 下列标识符(名字)命名原则中,符合规范的是( D )。
A. 类名的首字母小写
B. 变量和方法名的首字母大写
C. 接口名的首字母小写
D. 常量完全大写
32. Which switch-case below is NOT correct( D )?
A. int i; switch (i) { case…}
B. String s; switch (s) { case…}
C. char c; switch © { case…}
D. boolean b; switch (b) { case…}
33. 下面( B )表达式可以得到x和y中的最大值。
A. x>y?y:x
B. x<y?y:x
C. x>y?(x+y):(x-y)
D. x==y?y:x
34. 编译运行以下程序后,关于输出结果的说明,正确的是( C )。

public class Main {
    public static void main(String[] args) {
        int x = 4;
        System.out.println("value is "+((x>4)?99.9:9));
    }
}

A. 输出结果为: value is 99.99
B. 输出结果为: value is 9
C. 输出结果为: value is 9.0
D. 编译错误
35. Which of the following lines will compile without warning or error( B )?
A. float f=1.3;
B. int i=10;
C.byte b=257;
D.boolean b=null;

三、填空题

1.For code below:

Loop1:
while ( true ) {        //    1
    for ( ; true; ) {
        if ( i ==2 )
            break Loop1;//    2
    }
    i=4;                //    3
}
i=5;                    //    4

After executing line 2, where will the program jump to?

4

2.阅读以下程序:

import java.util.Scanner;
public class Test {
  public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number, max;
        number = input.nextInt();
        max = number;
        while (number != 0) {
            number = input.nextInt();
            if (number > max)
                 max = number;
        }
        System.out.println("max is " + max);
        System.out.println("number " + number);
     }
}

假设输入是 2 3 4 5 0,程序输出是:

max is 5
number 0

3.请写出以下程序运行结果:

public class MyFor{
    public static void main(String argv[]){
        int i, j;
outer:    for (i=1;i <3;i++)
inner:    for (j=1; j<3; j++) {
        if (j==2)continue outer;
        System.out.println("Value for i=" + i + " Value for j=" +j);
        }
    }
}

Value for i=1 Value for j=1

Value for i=2 Value for j=1

四、fn函数题

是否偶数 (10 分)

本题要求实现一个函数,判盘输入的整数是否是偶数,如果是偶数,返回true,否则返回false。

函数接口定义:
 public static boolean isOdd(int data)

说明:其中 data 是用户传入的参数。 data 的值不超过int的范围。函数须返回 true 或者 false

裁判测试程序样例:
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int data=in.nextInt();
        System.out.println(isOdd(data));
    }


    /* 请在这里给出isOdd(i)函数 */


}
输入样例:

8

输出样例:

true

答案代码如下:

public static boolean isOdd(int data){
		if(data%2==0)
			return true;
		else
			return false;
}

五、编程题

1 求最大值 (10 分)

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

输入格式:

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

输出格式:

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

输入样例:

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

18 -299

输出样例:

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

18

答案代码如下:

import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int a=in.nextInt();
		int b=in.nextInt();
		if(a>b)
			System.out.println(a);
		else
			System.out.println(b);	
	}
}

2 编程题:统计符合条件元素的个数-hebust (10 分)

统计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);
    }  
}

3 编程题:判断闰年-hebust (10 分)

根据输入的正整数y所代表的年份,计算输出该年份是否为闰年 闰年的判断标准:
能够被4整除且不能被100整除的年份
或者能够被400整除的年份

输入格式:

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

输出格式:

是闰年,输出 yes
非闰年,输出 no

输入样例:

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

100

输出样例:

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

no

答案代码如下:

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

4 Time Difference (10 分)

What is the time difference between 10:30 and 11:45?
Your program reads two time spots and prints the time difference between them, in terms of hours and minutes.

Input Format

Two time spots, in 24-hour, each is represented as two numbers, as “hour minute”. The second time spot is later than the first and both are within the same day.

Output Format

Two numbers represent the time difference. The first is the hours in the difference, while the second is the minutes.

Sample Input

10 30 11 45

Sample Output

1 15

答案代码如下:

import java.util.Scanner;
public class Main {
	public static void main(String[] args){
	Scanner coin = new Scanner(System.in);
	int m = coin.nextInt();
	int n = coin.nextInt();
	int a = coin.nextInt();
	int b = coin.nextInt();
	int j,t;
    	if(m>a){
        	t=m;
        		m=a;
        			a=t;
    } 	j=a-m;
        	int k=b-n;
        		if(k<0){
            		b=b+60;
            		a=a-1;
            		j=j-1;
            		k=b-n;
    }
		System.out.println(j+" "+k);
	}
}

5 java基本语法-整数四则运算 (10 分)

输入2个整数,输出它们的和、差、乘积和准确的商。

输入格式:

输入两个整数

输出格式:

每一行中依次输出四则运算的结果

输入样例:

70
16

输出样例:

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

86
54
1120
4.375

答案代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();
        System.out.println(num1 + num2);
        System.out.println(num1 - num2);
        System.out.println(num1 * num2);
        System.out.println((double)num1 / (double)num2);
    }
}

6 学投资 (10 分)

小白学习了一些复利投资知识,想比较一下复利能多赚多少钱(所谓复利投资,是指每年投资的本金是上一年的本金加收益。而非复利投资是指每年投资金额不包含上一年的收益,即固定投资额)。假设他每年固定投资M元(整数),每年的年收益达到P(0<P<1,double),那么经过N(整数)年后,复利投资比非复利投资多收入多赚多少钱呢?计算过程使用双精度浮点数,最后结果四舍五入输出整数(Math的round函数)。

输入格式:

M P N

输出格式:

复利收入(含本金),非复利收入(含本金),复利比非复利收入多的部分(全部取整,四舍五入)

输入样例:

10000 0.2 3

输出样例:

17280 16000 1280

答案代码如下:

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int M = sc.nextInt();
		double P = sc.nextDouble();
		int N = sc.nextInt();
		double money1=0 , money2=0 ;
		money1 = M*(Math.pow(1+P, N));
		money2 = M + M*P*N;
		System.out.print(Math.round(money1)+" "+Math.round(money2)+" "+Math.round(money1-money2));		
	}
}

7 点是否在圆内? (10 分)

编写程序,提示用户输入一个点(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);
        String s = scanner.nextLine();
        String[] coord = s.split("\\s+");
        int x = Math.abs(Integer.parseInt(coord[0]));
        int y = Math.abs(Integer.parseInt(coord[1]));
        double radius = Math.sqrt(x * x + y * y); 
        if (radius<10){
            System.out.println(1);
        }else {
            System.out.println(0);
        }
    }
}

8 给出一个月的总天数 (10 分)

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

输入格式:

输入任意符合范围(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);
	}
}

9 程序改错题1 (10 分)

程序改错题。以下代码目标是实现从键盘输入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);
    }   
}

10 成绩分级管理 (10 分)

学校进行成绩分级管理,取消分数制,改为成绩分级评定。具体办法是:小于60分为E类;60分至70分(不含70分)为D类;70分至80分(不含)为C类;80分至90分(不含)为B类;90分以上为A类。设计一个程序,对输入的成绩进行等价划分

输入格式:

输入一个整数表示成绩。

输出格式:

根据输入的成绩,输出对应的等级(大写字母)

输入样例:

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

72

输出样例:

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

C

答案代码如下:

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		int n;
		Scanner input=new Scanner(System.in);
		n=input.nextInt();
		if(n<60)
		{
			System.out.println("E");
		}
		if(n>=60&&n<70)
		{
			System.out.println("D");
		}
		if(n>=70&&n<80)
		{
			System.out.println("C");
		}
		if(n>=80&&n<90)
		{
			System.out.println("B");
		}
		if(n>=90)
		{
			System.out.println("A");
		}
	}
}

11 Java中二进制位运算 (10 分)

本题目要求读入2个整数和一个字符,然后根据这个字符值,对两个整数进行相应的二进制位的运算。要求必须使用switch选择结构
(1)如果字符是&,则两个整数进行二进制位的与运算
(2)如果字符是 |,则两个整数进行二进制位的或运算
(3)如果字符是^,则两个整数进行二进制位异或运算
(4)如果是其他字符,则固定输出信息:ERROR

输入格式:

在一行中依次输入整数1,字符,整数2。

输出格式:

类似3 & 4 = 0
其中,运算符号&的前后都有一个空格,等号的前后也都有一个空格。 上面表示3和4做二进制的与运算,结果是0。

输入样例:

3的二进制是0011,4的二进制是0100,二者与运算的结果是0。

3 & 4

输出样例:

注意&和=的前后,都是有且仅有一个空格。

3 & 4 = 0

输入样例:

7 X 3

输出样例:

ERROR

答案代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String [] arr = str.split("\\s+");
        int num1 = Integer.parseInt(arr[0]);
        int num2 = Integer.parseInt(arr[2]);
        switch (arr[1]){
            case "&":
                System.out.println(num1 + " & " + num2 + " = " + (num1&num2));
                break;
            case "|":
                System.out.println(num1 + " | " + num2 + " = " + (num1|num2));
                break;
            case "^":
                System.out.println(num1 + " ^ " + num2 + " = " + (num1^num2));
                break;
            default:
                System.out.println("ERROR");
                break;
        }
    }
}

12 JAVA-求整数序列中出现次数最多的数 (10 分)

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

输入格式:

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

输出格式:

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

输入样例:

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

10
3
2
-1
5
3
4
3
3
2

输出样例:

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

3 4

答案代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        int i, j, n;
        int max;
        int index = 0;
        n=sr.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        for (i = 0; i < n; i++)
        {
            a[i]=sr.nextInt();
        }
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (a[i] == a[j])
                {
                    b[i]++;
                }
            }
        }
        max = b[0];
        for (i = 1; i < n; i++)
        {
            if (max < b[i])
            {
                max = b[i];
                index = i;
            }
        }
        System.out.printf("%d %d\n",a[index], max);
    }
}

13 统计正数和负数的个数然后计算这些数的平均值 (10 分)

编写程序,输入未指定个数的整数,判断读入的正数有多少个,读入的负数有多少个,然后计算这些输入值得总和及平均值(不对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 input = new Scanner(System.in);		
		int positiveNum = 0; 
		int	negativeNum = 0; 
		int sum = 0; 		
		while (true) {
			int x = input.nextInt();
			if (x == 0)  
				break;			
			if (x > 0) 
				positiveNum++;	
			else 
				negativeNum++;			
			sum += x;
		}		
		if (positiveNum + negativeNum != 0) {	
			System.out.println(positiveNum);
			System.out.println(negativeNum);
			System.out.println(sum);			
			double average = 0.0;
			average = 1.0 * sum / (positiveNum + negativeNum);
			System.out.println(average);
		}
	}
}

14 分队列 (10 分)

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

输入格式:

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

输出格式:

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

输入样例:

11

输出样例:

1 3 5 7 9 11

答案代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
        String n=input.readLine();
        int rn=Integer.parseInt(n);
        for(int i=1;i<=rn;i++){
            if(i==1)System.out.print(1);
            if(i!=1&&i%2!=0)System.out.print(" "+i);
        }
    }
}

15 累加器 (10 分)

请你实现一个累加器。输入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);
    }
}

16 我是升旗手 (10 分)

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

输入格式:

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

输出格式:

输出身高最高的同学的身高。

输入样例:

4
130 125 129 140

输出样例:

140

答案代码如下:

import java.util.Scanner;
public class Main {    
public static void main(String args[]) {         
	Scanner sc = new Scanner(System.in);        
	int num=Integer.valueOf(sc.nextLine());  
	int s[]=new int[num];                
	int max=-1;        
	 for (int i=0;i<num;i++){             
		 s[i]=sc.nextInt();                         
		 if (s[i]>max){                             
		 max=s[i];                             
	 }                     
	 }                 
	 System.out.println(max);   
	 }
 }

17 等腰直角三角形 (10 分)

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

*

n=2,输出:

*
**

n=3,输出:

*
**
***

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

输入格式:

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

输出格式:

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

输入样例:

4

输出样例:

*
**
***
****

答案代码如下:

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

18 666 (10 分)

中国人非常喜欢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 sc=new Scanner (System.in);
			int n=sc.nextInt();
			int sum=0;
			int sum1=0;
			for(int i=1;i<=n;i++) {
				sum=sum*10+6;
				sum1=sum1+sum;
			}
		System.out.println(sum1);		
	}
}

19 倒顺数字串 (10 分)

输入正整数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 count=scanner.nextInt();
        if (count==1){
            System.out.println(1);
            return;
        }
        int i,j;
        for (i=1;i<=count;i++){
            System.out.print(i+" ");
        }
        for (j=count-1;j>1;j--){
            System.out.print(j+" ");
        }
        System.out.println(j);
    }
}

20 上三角数字三角形 (10 分)

输入一个正整数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 sc = new Scanner(System.in);
        int n = sc.nextInt();
        int num=1 ;
        for (int i = 0; i < n ; i++) {
            for (int j = 0; j <n-i; j++) {
                System.out.printf("%4d",num++);
            }
            System.out.println();
        }
    }
}

21 求质数 (10 分)

本题目要求输入一个大于等于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+" ");
        }
    }
}
  • 14
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是程序改错后的代码: #include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int arr[10]; int i, max_index = 0, min_index = 0; printf("请输入10个整数:\n"); for (i = 0; i < 10; i++) { scanf("%d", &arr[i]); if (arr[i] > arr[max_index]) { max_index = i; } if (arr[i] < arr[min_index]) { min_index = i; } } swap(&arr[max_index], &arr[min_index]); printf("调整后的数组为:\n"); for (i = 0; i < 10; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } 程序的思路是先从键盘输入10个整数,同时记录最大值和最小值的下标,然后调用swap函数将最大值和最小值的位置对换,最后输出调整后的数组。 需要注意的是,swap函数的参数是指针类型,因为我们需要修改数组中的元素值,而不是仅仅交换指针的值。在调用swap函数时,需要传入最大值和最小值的地址,即&arr[max_index]和&arr[min_index]。 另外,程序中的输出语句需要换行,以便更好地显示结果。 希望对你有帮助! ### 回答2: 这道题需要将程序进行改错,所以需要先分析出程序中存在的问题,再进行修正。 首先,程序中需要从键盘上任意输入10个整数,我们可以使用循环来实现。具体的实现方法如下: ```c int arr[10]; // 定义长度为10的数组 for (int i = 0; i < 10; i++) { scanf("%d", &arr[i]); // 从键盘输入整数,并存储到数组中 } ``` 其次,题目要求使用函数编程实现将最大数与最小数的位置进行对换。我们可以编写一个函数来实现该功能。具体的实现方法如下: ```c void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void swapMaxMin(int arr[], int len) { int maxIndex = 0; int minIndex = 0; // 遍历数组,找出最大数和最小数的下标 for (int i = 1; i < len; i++) { if (arr[i] > arr[maxIndex]) { maxIndex = i; } if (arr[i] < arr[minIndex]) { minIndex = i; } } // 交换最大数和最小数的位置 swap(&arr[maxIndex], &arr[minIndex]); } ``` 最后,将数组输出即可。具体的实现方法如下: ```c for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); // 输出调整前的数组 } swapMaxMin(arr, 10); // 调用函数,将最大数和最小数的位置进行对换 printf("\n"); for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); // 输出调整后的数组 } ``` 完整代码如下: ```c #include <stdio.h> // 交换两个数的值 void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // 将数组中最大数和最小数的位置进行对换 void swapMaxMin(int arr[], int len) { int maxIndex = 0; int minIndex = 0; // 遍历数组,找出最大数和最小数的下标 for (int i = 1; i < len; i++) { if (arr[i] > arr[maxIndex]) { maxIndex = i; } if (arr[i] < arr[minIndex]) { minIndex = i; } } // 交换最大数和最小数的位置 swap(&arr[maxIndex], &arr[minIndex]); } int main() { int arr[10]; // 定义长度为10的数组 for (int i = 0; i < 10; i++) { scanf("%d", &arr[i]); // 从键盘输入整数,并存储到数组中 } for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); // 输出调整前的数组 } swapMaxMin(arr, 10); // 调用函数,将最大数和最小数的位置进行对换 printf("\n"); for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); // 输出调整后的数组 } return 0; } ``` ### 回答3: 题目要求我们从键盘输入10个整数,并用函数编程实现将其中最大数与最小数的位置对换后输出调整后的数组。这个问题可以分成两个步骤:输入10个整数,找到其中的最大数与最小数并交换它们的位置。 首先我们需要从键盘输入10个整数,这可以通过使用循环和scanf函数实现。具体来说,我们可以使用一个for循环,每次循环向用户询问一个整数,直到询问了10个整数为止。 接下来,我们需要找到最大数和最小数,并将它们的位置对调。这可以通过两个分别找到最大值和最小值的函数实现。具体来说,我们可以编写两个函数,一个函数用于找到最大值,另一个函数用于找到最小值。在这两个函数中,我们可以使用一个for循环遍历数组中的所有元素,每次将当前元素与当前最大值或最小值进行比较,更新当前最大值或最小值的位置。然后我们将找到的最大值和最小值交换位置,以完成题目要求。 最后,我们需要输出调整后的数组。这可以通过使用一个for循环遍历数组中的所有元素,并使用printf函数将它们输出实现。这样,我们就完成了题目要求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值