解题思路:
用一个长度为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分
38 人评分
你们这表达是不是太麻烦了点,判断闰年我都记住这一个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; }
C语言程序设计教程(第三版)课后习题7.2 (Java代码)浏览:687 |
【回文数(二)】 (C++代码)浏览:879 |
C二级辅导-进制转换 (C语言代码)浏览:629 |
买不到的数目 (C++代码)浏览:869 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:915 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:714 |
WU-陶陶摘苹果2 (C++代码)浏览:980 |
三角形 (C语言代码)浏览:928 |
简单的a+b (C语言代码)浏览:435 |
矩形面积交 (C++代码)浏览:1142 |
零度 2019-01-19 18:59:52 |
没毛病