解题思路:
用一个长度为12的数组,记录每个月的天数,除了2月先不记录;
输入日期,判断闰年2月赋值29,平年28;
计算是第几天,输出;
参考代码1:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; /* */ struct date { int year; int month; int day; }; int main(int argc,char* argv[]) { struct date d1; int sum=0; cin>>d1.year>>d1.month>>d1.day; switch(d1.month) { case 1:break; case 2:sum=sum+31;break; case 3:sum=sum+31+29;break; case 4:sum=sum+31+29+31;break; case 5:sum=sum+31+29+31+30;break; case 6:sum=sum+31+29+31+30+31;break; case 7:sum=sum+31+29+31+30+31+30;break; case 8:sum=sum+31+29+31+30+31+30+31;break; case 9:sum=sum+31+29+31+30+31+30+31+31;break; case 10:sum=sum+31+29+31+30+31+30+31+31+30;break; case 11:sum=sum+31+29+31+30+31+30+31+31+30+31;break; case 12:sum=sum+31+29+31+30+31+30+31+31+30+31+30;break; default:cout<<"输入有误\n"; } sum=sum+d1.day; if(!((d1.year%4==0&&d1.year%100!=0)||d1.year%400==0)) { sum--; } cout<<sum; return 0; }
参考代码2:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; /* */ struct date { int year; int month; int day; }; int main(int argc,char* argv[]) { struct date d1; int sum=0; cin>>d1.year>>d1.month>>d1.day; int a[12]={31,0,31,30,31,30,31,31,30,31,30,31}; if((d1.year%4==0&&d1.year%100!=0)||d1.year%400==0) a[1]=29; else a[1]=28; for(int i=0;i<d1.month-1;i++) { sum+=a[i]; } sum+=d1.day; cout<<sum; return 0; }
别忘点赞哦
0.0分
0 人评分