西南科技大学oj题66
定義一個結構體變量(包括年、月、日),編程序,要求輸入年月日,計算并輸出該日
在本年中第幾天。注意本題一定要定義結構體變量,否則本實驗成績無效。
輸入
輸入三個整數(并且三個整數是合理的,既比如當輸入月份的時候應該在1 至12 之間,
不應該超過這個范圍)否則輸出Input error!
輸出
輸出一個整數.既輸入的日期是本月的第幾天。
樣例輸入
1985 1 20 2006 3 12樣例輸出
20 71
這個題目具有很多坑,
1.如題目說的只有12個月,如果是其他不合法的輸出是Input error!
2.如果是閏年并且月份超過二月,則二月是有29天
3.輸入的最后一個數字不能比那個月的天數多,也不能小于1
最后代碼附上
#include<stdio.h>
struct days{
? ? int year;
? ? int mouth;
? ? int day;
};
bool ruinian(int year)
{
? ? if(year%4==0&&year%100!=0)
? ? ? ? {
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? else if(year%400==0)
? ? ? ? {
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? else?
? ? ? ? {
? ? ? ? ? ? return false;
? ? ? ? }
}
int main()
{
? ? ? ? struct days str;
? ? ? ? while(scanf("%d%d%d",&str.year,&str.mouth,&str.day)!=EOF)
? ? ? ? {
? ? ? ? if(str.mouth<=0||str.mouth>12)?
? ? ? ? {
? ? ? ? ? ? printf("Input error!\n");
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? int sum=0;
? ? ? ? int b[12]={31,28,31,30,31,30,31,31,30,31,30,31};
? ? ? ? if(ruinian(str.year)==true)?
? ? ? ? ? ? b[1]=29;
? ? ? ? if(str.day >b[str.mouth-1]||str.day<1)
? ? ? ? {
? ? ? ? ? ? printf("Input error!\n");
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? for(int i=0;i<str.mouth-1;i++)
? ? ? ? {
? ? ? ? ? ? sum+=b[i];
? ? ? ? }
? ? ? ? sum+=str.day;
? ? ? ? printf("%d\n",sum);
? ? ? ? }
? ? return 0;
}
總結
以上是生活随笔為你收集整理的西南科技大学oj题66的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机精品在线开放课程申报书,精品在线开
- 下一篇: “2020 RT-Thread开发者大会