题目
输入:YYYY MM DD
计算到20211024程序员节的这一天的
Day数
代码:
JAVA版本:
import java.util.Scanner;
public class App {
static boolean IsLeapYear(int y) {
return (y % 4 == 0 || (y % 100 == 0 && y % 400 == 0));
}
static int OriYear, OriMon, OriDay;
static int[] MonthDay1 = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int[] MonthDay2 = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int AnsDay = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
OriYear = scan.nextInt();
OriMon = scan.nextInt();
OriDay = scan.nextInt();
scan.close();
int d = OriDay, m = OriMon;
for (int y = OriYear; y <= 2021; y++) {
int toM = (y == 2021 ? 10 : 12);
for (; m <= toM; m++) {
int temp = IsLeapYear(y) ? MonthDay2[m] : MonthDay1[m];
if (y == 2021 && m == 10)
temp = 24;
for (; d <= temp; d++) {
AnsDay++;
}
d = 1;
}
m = 1;
}
System.out.println(AnsDay);
}
}
C++版本:
#include <iostream>
using namespace std;
bool IsLeapYear(int y) { return (y % 4 == 0 || (y % 100 == 0 && y % 400 == 0)); }
int OriYear, OriMon, OriDay;
int MonthDay1[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int MonthDay2[15] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int AnsDay = 0;
int main() {
cin >> OriYear >> OriMon >> OriDay;
int d = OriDay, m = OriMon;
for (int y = OriYear; y <= 2021; y++) {
int toM = (y == 2021 ? 10 : 12);
for (; m <= toM; m++) {
int temp = IsLeapYear(y) ? MonthDay2[m] : MonthDay1[m];
if (y == 2021 && m == 10)
temp = 24;
for (; d <= temp; d++) {
AnsDay++;
}
d = 1;
}
m = 1;
}
cout << AnsDay;
return 0;
}
解题思路:炒鸡简单,三重循环直接解决。最外层是Year内层是Month,最内层是Day。
今天也是懒惰的小熊一天,爱关注不关注