给定年月日计算是那一天

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
37
38
39
40
41
42
43
44
45
public class Day {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()){
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
int daysInYear = getDaysInYear(year, month, day);
System.out.println(daysInYear);
}
}

private static int getDaysInYear(int year, int month, int day) {

int total = 0;

switch (month-1){
case 11:total += 30;
case 10:total += 31;
case 9:total += 30;
case 8:total += 31;
case 7:total += 31;
case 6:total += 30;
case 5:total += 31;
case 4:total += 30;
case 3:total += 31;
case 2:total += 28;
case 1:total += 31;
case 0:break;
}
if (isR(year)){
total++;
}
total +=day;
return total;
}

private static boolean isR(int year){
if(year%400==0||(year%4==0 && year%100!=0)){
return true;
}
return false;
}

}

计算回文素数

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
public class Sushu {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();

for (int i=a;i<=b;i++){
if (isPrime(i) && isHuiO(i)){
System.out.println(i);
}
}
}
//判断是不是素数
private static boolean isPrime(int n){
for (int i = 2;i<Math.sqrt(n);i++){
if (n % i ==0){
return false;
}
}
return true;
}

private static boolean isHuiO(int n){
String s = String.valueOf(n);
for (int i = 0;i<s.length()/2;i++){
if (s.charAt(i)!=s.charAt(s.length()-i-1)){
return false;
}
}
return true;
}
}