文章

64

粉丝

53

获赞

3

访问

14.1k

头像
日期计算 题解:
P1051 中南大学机试题
发布于2024年3月11日 15:29
阅读数 302

1051:为何正确率一直是50%呢

struct date{
     int year;
     int month;
     int day;
 }p;
 #include <bits/stdc++.h>
 using namespace std;
 int main()
 {
     int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     int aus=0;
     
     while(cin>>p.year>>p.month>>p.day)
     {
         aus=0;
         if(p.month>12||p.month<0)//判断月份是否符合要求
         printf("Input error!");
         
        /*if(p.month==1||p.month==3||p.month==5||p.month==7||p.month==8||p.month==10||p.month==12){
            if(p.day>31)
      ...

登录查看完整内容


登录后发布评论

4 条评论
小酒
2024年3月12日 15:18

struct date{
 	int year;
 	int month;
 	int day;
 }p;
 #include <bits/stdc++.h>
 using namespace std;
 int main()
 {
	 while(cin>>p.year>>p.month>>p.day)
 	{
	 	int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	 	
 		if(p.month>12||p.month<0||p.day<0||p.day>a[p.month])//判断月份是否符合要求 
 		printf("Input error!\n");
 		else
 		{	int aus=0;
 			if((p.year%4==0&&p.year%100!=0)||p.year%400==0)//判断是否为闰年 
 				a[2]=29;
 			else
 				a[2]=28;
 			for(int i=1;i<p.month;i++)
 			{
 				aus=aus+a[i];
			 }
			 aus=aus+p.day;
			 printf("%d\n",aus);
		 }
	 }
 }

 

赞(0)

小酒 : 回复 小酒: 之前发错了,发现判断输入月份是否符合要求出错了,应该这样写就对了。p.day>a[p.month]

2024年3月12日 15:20
snake
2024年3月11日 17:14

这个代码Input error之后还会继续计算输出

赞(0)

liutongzhao : 回复 snake: 感谢

2024年3月12日 13:22