Java_上机1

编程题目

  • 注意
    1. 下面代码块直接复制粘贴应该就可以通过
    2. 如果显示的是没有通过(如:unknown erro,wrong erro….)建议在评判界面等10秒左右,因为系统判定会有延迟。
    3. 如果对于改题您有更好的解决方式,欢迎联系本人

1.判断并显示闰年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main {
public static void main(String[] args) {
int yearNum = 2000;
for (int i = 0; yearNum < 2100; yearNum++) {
if(yearNum%4 == 0){
if(i == 5){
System.out.println();
i = 0;
}
System.out.print(yearNum+" ");
i++;
}
}
}
}

2.整数拆分求立方

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

public class Main {
public static void main(String[] args) {
int sum = 0;
Scanner scanner = new Scanner(System.in);
int scanNum;
while (scanner.hasNext()) {
sum = 0;
scanNum = scanner.nextInt();
while (scanNum != 0) {
sum += (scanNum % 10) * (scanNum % 10) * (scanNum % 10);
scanNum = scanNum / 10;
}
System.out.print(sum);
}
}
}

3.编写程序,输出成绩提高的百分比。

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

public class Main {
public static void main(String[] args) {
double rate = 0;
double a,b;
Scanner scanner = new Scanner(System.in);
int scanNum=1;
while (scanner.hasNext()) {
a = scanner.nextDouble();
b = scanner.nextDouble();
rate = (b-a)/a;
System.out.print(String.format("%.2f", rate*100)+"%");
}
}
}

4.a+b

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) {
int a,b;
int sum = 0;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
a = scanner.nextInt();
b = scanner.nextInt();
sum = a + b;
System.out.print(sum);
}
}
}

5.输出时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int a;
int HH,MM,SS;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
SS = a % 60;
MM = ((a-SS)/60) % 60;
HH = a/3600;
System.out.print(HH+" "+MM+" "+SS);
while(scanner.hasNext()){
System.out.println();
a = scanner.nextInt();
SS = a % 60;
MM = ((a-SS)/60) % 60;
HH = a/3600;
System.out.print(HH+" "+MM+" "+SS);

}
}
}

6.摄氏温度和华氏温度的转换

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

public class Main {
public static void main(String[] args) {
double huaShiTemperature;
double sheShiTemperature;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
sheShiTemperature = scanner.nextDouble();
huaShiTemperature = (double)sheShiTemperature*9/5+32;
System.out.print(String.format("%.2f", huaShiTemperature));
}
}
}

7. 输出天数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int year,month,day;
int sum;
int[] a = new int[13];
a[1] = 31;
a[2] = 28;
a[3] = 31;
a[4] = 30;
a[5] = 31;
a[6] = 30;
a[7] = 31;
a[8] = 31;
a[9] = 30;
a[10] = 31;
a[11] = 30;
a[12] = 31;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
sum = 0;
year = scanner.nextInt();
month = scanner.nextInt();
day = scanner.nextInt();
if(year % 4 == 0){
a[2] = 29;
}
for (int i = 1; i < month ; i++) {
sum += a[i];
}
sum += day;
System.out.print(sum);
}
}
}

8. 求绝对值

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

public class Main {
public static void main(String[] args) {
double num;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
num = scanner.nextDouble();
num = abs(num);
System.out.println(String.format("%.2f",num));
}
}

private static double abs(double num) {
if (num < 0){
return -num;
}
return num;
}
}