問題描述
通過鍵盤輸入某年某月某日,判斷這一天是這一年的第幾天
分析
要想判斷是當年的第幾天,要知道這月前每個月的天數(可能和年份有關),然后加上當月的日期就可以了。
每月的天數隨年份變化的,主要是涉及到二月,即閏年問題。
首先判斷月份,如果是一月、二月,無需判斷是不是閏年;三月及以后月份需要對年份判斷,相應計算天數。
源程序
C語言
#include
int main()
{
int year,month,day,leap=0,days=0;
printf("請輸入年、月、日,中間以空格隔開:\n");
/*
//這樣也可以
scanf("%d",&year);
scanf("%d",&month);
scanf("%d",&day);
*/
scanf("%d %d %d",&year,&month,&day);
//判斷是否為閏年,是的話多加一天
if(month>2)
if((year%400==0)||(year%4==0&&year%100!=0))
leap = 1;
switch(month)
{ case 1:days=0;break; //一月之前的天數為0
case 2:days=31;break;
case 3:days=31+28;break;
case 4:days=31+28+31;break;
case 5:days=31+28+31+30;break;
case 6:days=31+28+31+30+31;break;
case 7:days=31+28+31+30+31+30;break;
case 8:days=31+28+31+30+31+30+31;break;
case 9:days=31+28+31+30+31+30+31+31;break;
case 10:days=31+28+31+30+31+30+31+31+30;break;
case 11:days=31+28+31+30+31+30+31+31+30+31;break;
case 12:days=31+28+31+30+31+30+31+31+30+31+30;break;
}
/*
//這一部分是當時寫程序時檢查用的,可以不寫
printf("year = %d\n",year);
printf("month = %d\n",month);
printf("day = %d\n",day);
printf("leap = %d\n",leap);
printf("days = %d\n",days);
*/
days = day+leap+days;
printf("這一天是%d年的第%d天",year,days);
return 0;
}
其中主體部分如果不使用break語句也可以這樣去寫:
switch(month)
{ case 12:days+=30; //11月的天數
case 11:days+=31;
case 10:days+=30;
case 9:days+=30;
case 8:days+=31;
case 7:days+=30;
case 6:days+=31;
case 5:days+=30;
case 4:days+=31;
case 3:days+=28;
case 2:days+=31;
case 1:days+=0;
}
python
使用python編程的思路和上面一致,同樣的代碼結構
#coding:gbk
while True:
#用戶輸入相關信息
year = int(input("請輸入年份:"))
month = int(input("請輸入月份:"))
day = int(input("請輸入幾號:"))
#當月份在二月后時,判斷是否閏年
if month>2:
if (year%400 == 0) or (year%100 != 0 and year % 4 ==0):
leap = 1
else:
leap = 0
#使用偽switch語句
def case1():
return 0
def case2():
return 0+31
def case3():
return 0+31+28
def case4():
return 0+31+28+31
def case5():
return 0+31+28+31+30
def case6():
return 0+31+28+31+30+31
def case7():
return 0+31+28+31+30+31+30
def case8():
return 0+31+28+31+30+31+30+31
def case9():
return 0+31+28+31+30+31+30+31+31
def case10():
return 0+31+28+31+30+31+30+31+31+30
def case11():
return 0+31+28+31+30+31+30+31+31+30+31
def case12():
return 0+31+28+31+30+31+30+31+31+30+31+30
def default():
return 100000
switch ={1:case1,
2:case2,
3:case3,
4:case4,
5:case5,
6:case6,
7:case7,
8:case8,
9:case9,
10:case10,
11:case11,
12:case12
}
days = switch.get(month,default)()
print("這一天是此年的第%d天"%(day+leap+days))
延伸
1.改進(數組的使用)
從上面兩個程序可以看出,在計算每個月的天數時重復了很多次,而對于月份來說,每個月的天數是固定的。因此可以考慮將12個月份的天數存儲在一個列表中(對于C語言來說是一維數組),使代碼變得整潔一些。
#coding:gbk
while True:
#用戶輸入相關信息
year = int(input("請輸入年份:"))
month = int(input("請輸入月份:"))
day = int(input("請輸入幾號:"))
#當月份在二月后時,判斷是否閏年
if month>2:
if (year%400 == 0) or (year%100 != 0 and year % 4 ==0):
leap = 1
else:
leap = 0
#計算之前月份的天數,先按平年進行計算
dayslist = [0,31,28,31,30,31,30,31,31,30,31,30]
days = 0
for mon in range(1,month):
days += dayslist[mon]
days = leap + days + day
print(str(leap))
print("這一天是此年的第%d天"%days)
#include
int main()
{
while(1)
{
int year,month,day,leap=0,days=0;
int dayslist[] = {0,31,28,31,30,31,30,31,31,30,31,30};//平年時每個月份之前每月的天數,使用一維數組存儲
printf("請輸入年、月、日,中間以空格隔開:\n");
scanf("%d %d %d",&year,&month,&day);
//判斷是否為閏年,是的話多加一天
if(month>2)
if((year%400==0)||(year%4==0&&year%100!=0))
leap = 1;
int mon;
for(mon=0;mon
|