ACM 2003~2005

时间:2023-06-08 17:19:57

ACM 2003 求实数的绝对值

import java.util.Scanner;

public class Lengxc {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
double d = scanner.nextDouble();
if (d < 0) {
d = -1 * d;
}
System.out.printf("%.2f",d);
System.out.println();
}
scanner.close();
}
}

ACM 2004 输入一个百分制的成绩,将其转换对应的等级,具体转换规则如下

import java.util.Scanner;

public class Lengxc {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int i = scanner.nextInt();
if (i < 0 || i > 100) {
System.out.println("Score is error!");
} else {
int s = (int) i / 10;
switch (s) {
case 10:
System.out.println("A");
case 9:
System.out.println("A");
break;
case 8:
System.out.println("B");
break;
case 7:
System.out.println("C");
break;
case 6:
System.out.println("D");
break;
default:
System.out.println("E");
break;
}
}
}
scanner.close();
}
}

ACM 2005 给定一个日期,输出这个日期是该年的第几天。输入格式:yyyy/MM/dd

import java.util.Scanner;

public class Lengxc {
public static void main(String[] args) {
int[] months = {31,28,31,30,31,30,31,31,30,31,30,31};
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String str = scanner.next();
String[] s = str.split("/");
int year = Integer.parseInt(s[0]);
int month = Integer.parseInt(s[1]);
int day = Integer.parseInt(s[2]);
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
months[1] = 29;
}else {
months[1] = 28;
}
int sum = 0;
for(int i = 0;i < month - 1;i++) {
sum = sum + months[i];
}
sum = sum + day;
System.out.println(sum);
}
scanner.close();
}
}