描述
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度: ,空间复杂度:O(1)
输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
while (sc.hasNext()) {
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
System.out.println(count(year, month, day));
}
}
}
public static int count(int year, int month, int day) {
int c = 1;
int sum = 0;
while (c < month) {
if (c == 1 || c == 3 || c == 5 || c == 7 || c == 8 || c == 10) {
sum += 31;
} else if (c == 2) {
if (year % 4 == 0 && year % 100 != 0) {
sum += 29;
} else {
sum += 28;
}
} else if (c == 4 || c == 6 || c == 9 || c == 11) {
sum += 30;
}
c++;
}
sum += day;
return sum;
}
}
输入:
2012 12 31输出:
366
输入:
1982 3 4输出:
63