| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 646 人关注过本帖
标题:我快崩溃啦!哪位达人帮忙看看我这个程序?
只看楼主 加入收藏
blowing00
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2009-11-9
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:7 
我快崩溃啦!哪位达人帮忙看看我这个程序?
我做一个题:输入一个整数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 编辑 ]
2009-11-09 00:38
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:15 
LZ把题目的链接发上来,可以让大家看看题目。

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-11-09 11:36
blowing00
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2009-11-9
收藏
得分:0 
这个是题目:

Friday the Thirteenth

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.

There are few facts you need to know before you can solve this problem:

January 1, 1900 was on a Monday.
Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday
INPUT FORMAT
One line with the integer N.
SAMPLE INPUT (file friday.in)
20

OUTPUT FORMAT
Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.
SAMPLE OUTPUT (file friday.out)
36 33 34 33 35 35 34

2009-11-09 12:50
zhu168a
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2009-11-9
收藏
得分:5 
你这算法不对啊,考虑下万年历程序了.
2009-11-09 18:04
blowing00
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2009-11-9
收藏
得分:0 
回复 4楼 zhu168a
嗯,我知道我这个程序有问题。但是不知道哪里错了,前面六组数据都过了,第七组过不了,似乎是数据量大了就会出错。我有用另外的方法写出对的,但是感觉跟这个没什么大区别,不知道错哪。能不能帮小弟指出错误,谢谢啦!
2009-11-09 19:48
blowing00
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2009-11-9
收藏
得分:0 
万年历,嗯,我去看看。
2009-11-09 19:54
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:0 
int leapYear(int y)               //判断是否平年,是的话返回0,否则返回1
{
    if ((y%4==0 && y%100!=0) || y&400==0)  //应该是%吧
        return 1;
    else return 0;
}

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-11-09 22:10
blowing00
Rank: 1
等 级:新手上路
帖 子:12
专家分:2
注 册:2009-11-9
收藏
得分:0 
回复 7楼 flyingcloude
原来是这个.....
太谢谢你了,这位达人,我终于可以进入下一节了。

[ 本帖最后由 blowing00 于 2009-11-9 23:11 编辑 ]
2009-11-09 23:09
快速回复:我快崩溃啦!哪位达人帮忙看看我这个程序?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027606 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved