题解 | #今年的第几天?#
今年的第几天?
https://www.nowcoder.com/practice/ae7e58fe24b14d1386e13e7d70eaf04d
#include <iostream> using namespace std; int judge_year(int year){ int count = 0; if(year%4==0&&year%100!=0||year%400==0){ return 1; } else return 0; } int main() { int year,mouth,day; int num1[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int num2[12]={31,29,31,30,31,30,31,31,30,31,30,31}; while (cin >> year >> mouth>>day) { // 注意 while 处理多个 case int count = 0; int flag = judge_year(year); if(flag){ for(int i = 0;i<mouth-1;i++){ count+=num2[i]; } } else{ for(int i = 0;i<mouth-1;i++){ count+=num1[i]; } } count+=day; cout<<count<<endl; } } // 64 位输出请用 printf("%lld")
设置两个数组,分别存储每个月的天数,但是要考虑是否为闰年。所以先判断,之后再按月份加到之前一个月,然后再加上当月天数。