题目:输入某年某月某日,判断该日是该年的第几天.
需要各位给个提示什么的,谢谢了!
#include<iostream>
#include<string>
using namespace std;
int str[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int str2[12]={31,28,31,30,31,30,31,31,30,31,30,31};
struct Year
{
int _Year;
int _month;
int _day;
};
int year(int _year)
{
if(_year%4==0 && _year%100!=0 || _year%100==0 && _year%400==0)
{
return str[12];
}
else
return str2[12];
}
int main()
{
int day,num=0,temp;
Year a;
while(cin >> a._Year >> a._month >> a._day)
{
year(a._Year);
if(year(a._Year)==str[12])
{
for(temp=0;temp<a._month;temp++)
num+=str[temp];
num+=a._day;
cout << num << endl;
}
if(year(a._Year)==str2[12])
{
for(temp=0;temp<a._month;temp++)
num+=str2[temp];
num+=a._day;
cout << num << endl;
}
}
return 0;
}
看看这个我写的,呵呵。有用的话你看看,不难的,思路很清楚的
C source file. No optimization is applied.
No arrays are used.
======================================================
#include <stdio.h>
/**
Jan. 1 is the 1st day of each year.
January is 1st month.
Day 1 is 1st day.
*/
int dayCounting(int year, int month, int day)
{
int loopYear = (year % 4==0 && year%100!=0) || (year % 400 == 0);
int whichDay = 1;
int daysOfFeb = (loopYear ? 29 : 28);
switch(month)
{
case 1:
whichDay = day;
break;
case 2:
whichDay = 31 + day;
break;
case 3:
whichDay = 31 + daysOfFeb + day;
break;
case 4:
whichDay = 31 + daysOfFeb + 31 + day;
break;
case 5:
whichDay = 31 + daysOfFeb + 31 + 30 + day;
break;
case 6:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + day;
break;
case 7:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ day;
break;
case 8:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ day;
break;
case 9:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + day;
break;
case 10:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + 30 + day;
break;
case 11:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + 30 + 31+ day;
break;
case 12:
whichDay = 31 + daysOfFeb + 31 + 30 + 31 + 30+ 31+ 31 + 30 + 31+ 30 + day;
break;
}
return whichDay;
}
int main()
{
printf("%d\n", dayCounting(2007, 7, 28));
return 0;
}