我快崩溃啦!哪位达人帮忙看看我这个程序?
我做一个题:输入一个整数N,求出从1900年到1900+N-1年期间内,每个月的十三号分别落在星期一,星期二,星期三,星期四,星期五,星期六,星期天的次数。是USACO上面的第三题,一个很简单的问题可是第七组数据就是过不了啊#include "fstream"
#include "iostream"
using namespace std;
int leapYear(int y) //判断是否平年,是的话返回0,否则返回1
{
if ((y%4==0 && y%100!=0) || y&400==0)
return 1;
else return 0;
}
int main()
{
int r;
int i;
int wholedays=0;
int n;
int Sunday=0;
int Monday=0;
int Tuesday=0;
int Wednesday=0;
int Thursday=0;
int Friday=0;
int Saturday=0;
ifstream fin("friday.in");
fin>>n;
for (i=1; i<=n; i++) //循环N次,对每一年的每个月的十三号做判断。 wholedays是一个计数器,
{ //累计每个月的十三号距离1900年一月一号过了多少天
wholedays+=13;
r=wholedays%7; //循环体中一共有十二个switch
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=31;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
if (leapYear(1900+i-1)==0)
{
wholedays+=28;
}
else wholedays+=29;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=31;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=30;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=31;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=30;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=31;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=31;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=30;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=31;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=30;
r=wholedays%7;
switch (r)
{
case 0: Sunday++; break;
case 1: Monday++; break;
case 2: Tuesday++; break;
case 3: Wednesday++; break;
case 4: Thursday++; break;
case 5: Friday++; break;
case 6: Saturday++; break;
}
wholedays+=18;
}
//输出
ofstream fout("friday.out");
fout<<Saturday<<' '<<Sunday<<' '<<Monday<<' '<<Tuesday<<' '<<Wednesday<<' '<<Thursday<<' '<<Friday<<endl;
fout.close();
fin.close();
return 0;
}
第七组数据输入256
期待输出440_439_438_438_439_439_439
我的输出441_437_439_438_440_438_439
[ 本帖最后由 blowing00 于 2009-11-9 13:00 编辑 ]