解题思路:
用一个长度为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 <stdio.h> #include <math.h> int isleapyear(int x) { if ((x % 4) == 0 && (x % 100) != 0 || (x % 400 == 0)) return 1; else return 0; } int main() { struct time { int year; int month; int day; }; struct time rain; scanf("%d", &rain.year); scanf("%d", &rain.month); scanf("%d", &rain.day); int days=0, leap; leap = isleapyear(rain.year); switch (rain.month) { case 12:days += 30; case 11:days += 31; case 10:days += 30;
typedef struct DATA{ int year; int mon_get; int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day; }Data; int main(){ Data data; int count_day = 0; //表示天数 int pos = 0; //标记是否为一月 scanf("%d %d %d", &data.year, &data.mon_get, &data.day); if ((data.year%4 == 0 && data.year%100 != 0) || data.year%400 == 0) //如果是世纪闰年 data.month[1] = 29; for (int i=1; i<data.mon_get; ++i){ count_day += data.month[i-1]; pos++; } count_day += data.day; if (pos == 0) count_day = data.day; printf("%d\n", count_day); return 0; } 这样为什么编译错误
/*先令sum等于键盘输入的天数,再加上输入月数的前面的月份的天数*/ #include<stdio.h> struct date { int year; int month; int day; }p; int main() { scanf("%d%d%d",&p.year,&p.month,&p.day); int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int sum=p.day;//sum等于输入的天数 for(int i=0;i<p.month-1;i++)//下标0开始 { sum+=a[i]; } //这里的if语句只有月份大于2,闰年的判断才有意义 if((p.year%4==0&&p.year%100!=0)||(p.year%400==0)&&p.month>2) { printf("%d",sum+1); } else { printf("%d",sum); } return 0; }
#include<stdio.h> int chuli(int n[12]){ int year,month,day,sum=0; scanf("%d%d%d",&year,&month,&day); // n[12]={31,28,31,30,31,30,31,30,31,30,31,30}; if((year%4==0&&year%100!=0)||(year%400==0)) n[1]=29; for(int i=0;i<month-1;i++){ sum=sum+n[i]; } sum=sum+day; return sum; } int main() { int n[12]={31,28,31,30,31,30,31,30,31,30,31,30}; printf("%d",chuli(n)); // while(~scanf("%d%d", &a, &b))printf("%d\n",a+b); return 0; }//有没有大佬指点一下,提交说我答案错误。。
uq_40487458443 2021-11-28 17:32:09 |
月份的天数搞错了,7,8月都是31天,9月是30天,10月是31天,11月是30天,12月是31天
#include<stdio.h> int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); int sum=0; int d[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if((a%4==0&&a%100!=0)||a%400==0) d[1]=29; for(int i=0;i<b-1;i++){ sum+=d[i]; } sum+=c; printf("%d",sum); return 0; } 我感觉这样简单一点
#include<stdio.h> typedef struct time { int mouth,year,day; }time_s; int main() { int T; T=0; time_s time; int a[12]={31,29,31,30,31,30,31,31,30,31,30,31}; scanf("%d%d%d",&time.year,&time.mouth,&time.day); if((time.year%100==0&&time.year%4==0)) { if(time.mouth==1) { T=T+time.day; }else{ T=T+time.day; for(int i=0;i<time.mouth-1;i++) { T=T+a[i]; } } }else{ a[1]=28; if(time.mouth==1) { T=T+time.day; }else{ T=T+time.day; for(int i=0;i<time.mouth-1;i++) { T=T+a[i]; } } } printf("%d",T); return 0; }
大佬们可以看看错误50%,错哪了? #include<stdio.h> struct number { int year; int month; int day; }; int main() { struct number num; scanf("%d%d%d",&num.year,&num.month,&num.day); int isprim,days=0; if((num.year%4) && !(num.year%100) || !(num.year%400)){ isprim=1; }else{ isprim=0; } int i; for(i=3;i<num.month;i++){//判断闰年 if(i%2 != 0){ days+=31; }else{ days+=30; } } days=days+num.day; if(isprim==1){ days=days+31+29; }else{ days=days+31+28; } printf("%d\n",days); return 0; }
苏沐橙 2020-12-15 21:12:53 |
for(i=3;i<num.month;i++){//判断闰年 if(i%2 != 0){ days+=31; }else{ days+=30; } }这个错了
#include<stdio.h> struct date { int year; int month; int day; }; int main() { struct date * p,i; int j,k=0; p=&i; scanf("%d%d%d",&p->year,&p->month,&p->day); int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; for(j=0;j<p->month-1;j++) { k+=a[j]; } if(p->year % 4 == 0 && p->year % 100 == !0 || p->year % 400== 0) { printf("%d",k+p->day+1); } else printf("%d",k+p->day); return 0; }
C语言程序设计教程(第三版)课后习题10.5 (C语言代码)浏览:767 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:721 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:530 |
C语言训练-求素数问题 (C语言代码)浏览:773 |
矩阵乘法 (C++代码)浏览:1662 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:639 |
C语言训练-尼科彻斯定理 (C语言代码)浏览:509 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:1314 |
WU-输入输出格式练习 (C++代码)浏览:1133 |
用筛法求之N内的素数。 (C语言代码)浏览:685 |