很經典的題目:
當給你某個日期,讓你計算該日期在該年中時第幾天。
這裡可以巧用switch語句。
程序如下:
[cpp]
int t_nMon;
int t_nDay;
cout << "輸入月:";
cin >> t_nMon;
cout << "輸入天:";
cin >> t_nDay;
int s_ndays = 0;
switch(t_nMon-1)
{
case 12:s_ndays += 31;
case 11:s_ndays += 30;
case 10:s_ndays += 31;
case 9:s_ndays += 30;
case 8:s_ndays += 31;
case 7:s_ndays += 31;
case 6:s_ndays += 30;
case 5:s_ndays += 31;
case 4:s_ndays += 30;
case 3:s_ndays += 31;
case 2:s_ndays += 28;
case 1:s_ndays += 31;
}
s_ndays = s_ndays + t_nDay;
cout << s_ndays <<endl;