解题思路:
题目主要要搞清楚什么是闰年,什么是平年,然后闰年每个月有多少天,平年每个月有多少天。
注意事项:
参考代码:
#include <bits/stdc++.h> using namespace std; struct date{ int year; int month; int day; }; int main() { struct date today; cin>>today.year; cin>>today.month; cin>>today.day; //判断是不是闰年 ,一般的年份除以4,有余数的是平年,没有余数的是闰年, //但如果是年份是整百的,那么该年份除以400,有余数的是平年,没有余数的是闰年。 bool tag; if(today.year%100!=0 && today.year%4==0||today.year%400==0) { tag=1; } else tag=0; //闰年1~12月分别为31天、29天、31天、30天、31天、30天、31天、31天、30天、31天、30天、31天 //平年1~12月分别为 31天、28天、31天、30天、31天、30天、31天、31天、30天、31天、30天、31天 int run[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //如果是闰年, int sum=0; if(tag==1){ for(int i=0;i<today.month-1;i++) { sum+=run[i]; } sum+=today.day; } //如果是平年 else { for(int i=0;i<today.month-1;i++) { sum+=ping[i]; } sum+=today.day; } cout<<sum<<endl; return 0; }
0.0分
0 人评分