原题链接:[编程入门]结构体之时间设计
解题思路:
用一个长度为12的数组,记录每个月的天数,除了2月先不记录;
输入日期,判断闰年2月赋值29,平年28;
计算是第几天,输出;
参考代码:
#include <stdio.h>
#include <iostream>
using namespace std;
class riqi {
private:
int year;
int month;
int day;
public:
riqi( int y, int m, int d )
{
year = y;
month = m;
day = d;
}
void count();
};
void riqi::count()
{
int n = 0;
int a[12];
a[0] = 31;
a[1] = 0;
a[2] = 31;
a[3] = 30;
a[4] = 31;
a[5] = 30;
a[6] = 31;
a[7] = 31;
a[8] = 30;
a[9] = 31;
a[10] = 30;
a[11] = 31;
if ( year % 100 == 0 ) //判断年数为2000这样的年份
{
if ( year % 400 == 0 )
a[1] = 29;
else
a[1] = 28;
}
if ( year % 4 == 0 && year % 100 != 0 ) //能被4整除但不能被100整除
a[1] = 29;
if ( year % 4 != 0 )
a[1] = 28;
for ( int i = 0; i < month - 1; i++ ) //数组下标从0开始,所以month-1
n = n + a[i];
n += day;
cout << n;
}
int main()
{
int Y, M, D;
cin >> Y;
cin >> M;
cin >> D;
riqi A( Y, M, D );
A.count();
return(0);
}几个月前写的,翻出来弄题解,讲的思路没有写的时候清晰~
别忘点赞哦-.-
0.0分
30 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
#include <stdio.h> int main(int argc, char *argv[]) { int year,month,date,lun,days; scanf("%d%d%d",&year,&month,&date); //輸入年月日 lun=(year%4 == 0); //适当的运用返回值1 switch(month) { case 12:days+=30; case 11:days+=31; case 10:days+=30; case 9:days+=31; case 8:days+=31; case 7:days+=30; case 6:days+=31; case 5:days+=30; case 4:days+=31; case 3:days+=28+lun; //利用lun来判断是否闰年,如是,则返回1;如不是,lun则等于0 case 2:days+=31; //如果不是一月的話再加一月的天数 case 1:days+=date;break;你们这表达是不是太麻烦了点,判断闰年我都记住这一个if就行了。 #include<stdio.h> struct much { int year; int mouth; int day; }; int main() { int sum=0,i; struct much today; int mou[12]={31,28,31,30,31,30,31,31,30,31,30,31}; scanf("%d %d %d",&today.year,&today.mouth,&today.day); if((today.year%4==0&&today.year%100!=0)||today.year%400==0) { mou[1]=29; } for(i=0;i<12;i++) { if(today.mouth!=i+1) { sum+=mou[i]; }else { sum+=today.day; break; } } printf("%d",sum); return 0; }