1182-C語言-數日子
阿新 • • 發佈:2019-01-08
Problem Description
光陰似箭,日月如梭,大學的時間真是寶貴,要抓緊時間AC_。你知道今天是這一年第幾天嗎,掐指一算還是要算好久,呵呵還是讓計算機來做吧。這裡的問題就是讓你來寫一個程式,輸入某年某月某日,判斷這一天是這一年的第幾天?
Input
輸入第一行是資料的組數n<100,下面n行是n組資料,每組資料由3個正整陣列成,分別為年、月、日,我們保證每組資料都是有效的日期。
Output
輸出所輸入的日期是這一年的第幾天。
Sample Input
2
2009 1 1
2008 1 3
Sample Output
1
3
原始碼:
#include <stdio.h>
int manth_day(int year,int manth,int day);
int main()
{
int n,year,manth,day,days;
scanf("%d",&n);
for (int i=1; i<=n; i++) {
scanf("%d%d%d",&year,&manth,&day);
days=manth_day(year,manth,day);
printf("%d\n",days);
}
return 0;
}
int manth_day(int year,int manth,int day){
int leap,i,sum=0;
int a[2][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},
};
leap=((year%4==0&&year%100!=0)||year%400==0);
for(i=1;i<manth;i++){
sum+=a[leap][i];
}
sum+=day;
return sum;
}
小結:
在書上看過例題,自己敲出來時候也挺簡單的。運用二維陣列把閏年、非閏年 每月的天數儲存起來。leap的使用很巧妙,其後的判斷若為真,即閏年,leap 為1,否則leap 為2。其中呼叫函式的功能是返回天數,其他就很簡單了。