解题思路:
用一个长度为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 人评分
#include<iostream> int main(int argc, char *argv[]){ int year,month,day; int sum = 0; std::cin>>year>>month>>day; if((year%4==0 && year%100!=0) || (year%400==0)){ sum+=1; } for(int i = 1; i<month; i++){ if(i==1 || i==3 || i==5 || i==7 || i==8 || i == 10 || i==12){ sum+=31; } else if(i == 2){ sum+=28; } else{ sum+=30; } } sum+=day; std::cout<<sum; }
#include<stdio.h> int leap_year(int y); struct year { int tear; int yue; int ru; }temp; int main() { int t=0,a=0; int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31}; int c[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; scanf_s("%d%d%d",&temp.tear,&temp.yue,&temp.ru); t = leap_year(temp.tear); if (t == 1) for (int i = 0; i < temp.yue-1; i++) a += c[i]; if (t == 0) for (int i = 0; i < temp.yue - 1; i++) a += b[i]; printf("%d",temp.ru+a); return 0; } int leap_year(int y) { if ((y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))) return 0; else return 1; } 欢迎点评
#include <stdio.h> struct Time { int year; int month; int day; }; int main(void) { struct Time time; int i, year, month, day; int sum = 0; int y[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; scanf("%d%d%d", &time.year, &time.month, &time.day); sum = time.day; for (i = 0; i < time.month - 1; i++) { sum += y[i]; } if ((time.year % 400 == 0) || (time.year % 4 == 0 && time.year % 100 != 0) && (time.month > 2)) printf("%d", sum + 1); else printf("%d", sum); return 0; }
21郁钰 2019-12-11 20:18:42 |
真.优质题解
有大佬帮忙看一下哪里错了吗?错误50% #include <iostream> using namespace std; struct Data { int year; int month; int day; }; int main() { Data time; int a[12]={31,28,31,30,31,30.31,31,30,31,30,31},day=0; cin>>time.year>>time.month>>time.day; if((time.year%4==0&&time.year%100!=0)||time.year%400==0) a[1]=29; for(int i=0;i<time.month-1;i++) day=day+a[i]; day=day+time.day; cout<<day<<endl; return 0; }
dragonZhan 2020-02-13 12:56:44 |
endl
#include <stdio.h> int main() { struct dd { int year; int month; int day; }st1; int sum = 0; int a[][13]={ {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; scanf ("%d%d%d",&st1.year,&st1.month,&st1.day); int leap = 0; leap = ( st1.year % 4 == 0 && st1.year %100 != 0 || st1.year % 400 == 0 ); while ( st1.month > 0){ st1.month--; sum += a[leap][st1.month]; } sum += st1.day; printf("%d",sum); }
#include<stdio.h> struct date{ int year; int month; int day; }aa; void main(){ int a[12]={31,28,31,30,31,30,31,31,30,31,30,31},t=0; scanf("%d%d%d",&aa.year,&aa.month,&aa.day); if(aa.year%100==0&&aa.year%400==0||aa.year%100!=0&&aa.year%4==0){ a[1]=29; } for(int i=0;i<aa.month-1;i++){ t=t+a[i]; } printf("%d\n",t+aa.day); }
#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;
C语言程序设计教程(第三版)课后习题11.8 (C语言代码)浏览:640 |
C语言程序设计教程(第三版)课后习题9.1 (Java代码)浏览:481 |
【数组的距离】 (C语言代码)浏览:787 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:790 |
简单的a+b (C语言代码)浏览:600 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:806 |
WU-判定字符位置 (C++代码)浏览:1471 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:583 |
蚂蚁感冒 (C语言代码)浏览:1408 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:537 |