| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 614 人关注过本帖
标题:请高手看一下错在哪里?
只看楼主 加入收藏
xiaowaibu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-3-23
结帖率:100%
收藏
已结贴  问题点数:3 回复次数:8 
请高手看一下错在哪里?
输入年,月,日,;输出星期几
#include <stdio.h>
main()
{
    int y,m,d,w;
    scanf("%d%d%d",&y,&m,&d);
    if((y%4==0&&y%100!=0)||y%400==0)
    {
        switch(m)
        {
        case 1:w=(365*y+(y/4-y/100+y/400)+d-2)%7;break;
case 2:w=(365*y+(y/4-y/100+y/400)+d+29)%7;break;
case 3:w=(365*y+(y/4-y/100+y/400)+d+58)%7;break;
case 4:w=(365*y+(y/4-y/100+y/400)+d+89)%7;break;
case 5:w=(365*y+(y/4-y/100+y/400)+d+119)%7;break;
case 6:w=(365*y+(y/4-y/100+y/400)+d+150)%7;break;
case 7:w=(365*y+(y/4-y/100+y/400)+d+180)%7;break;
case 8:w=(365*y+(y/4-y/100+y/400)+d+211)%7;break;
case 9:w=(365*y+(y/4-y/100+y/400)+d+242)%7;break;
case 10:w=(365*y+(y/4-y/100+y/400)+d+272)%7;break;
case 11:w=(365*y+(y/4-y/100+y/400)+d+303)%7;break;
case 12:w=(365*y+(y/4-y/100+y/400)+d+333)%7;
        }
    }
        else
        {
        switch(m)
    {

    case 1:w=(365*y+(y/4-y/100+y/400)+d-1)%7;break;
    case 2:w=(365*y+(y/4-y/100+y/400)+d+30)%7;break;
    case 3:w=(365*y+(y/4-y/100+y/400)+d+58)%7;break;
    case 4:w=(365*y+(y/4-y/100+y/400)+d+89)%7;break;
    case 5:w=(365*y+(y/4-y/100+y/400)+d+119)%7;break;
    case 6:w=(365*y+(y/4-y/100+y/400)+d+150)%7;break;
    case 7:w=(365*y+(y/4-y/100+y/400)+d+180)%7;break;
    case 8:w=(365*y+(y/4-y/100+y/400)+d+211)%7;break;
    case 9:w=(365*y+(y/4-y/100+y/400)+d+242)%7;break;
    case 10:w=(365*y+(y/4-y/100+y/400)+d+272)%7;break;
    case 11:w=(365*y+(y/4-y/100+y/400)+d+303)%7;break;
    case 12:w=(365*y+(y/4-y/100+y/400)+d+333)%7;
    }
        }
    switch(w)
    {
    case 1:printf("1\n");break;
case 2:printf("2\n");break;
case 3:printf("3\n");break;
case 4:printf("4\n");break;
case 5:printf("5\n");break;
case 6:printf("6\n");break;
case 0:printf("7\n");
    }
}
搜索更多相关主题的帖子: 400 include 
2010-04-25 14:16
longyi3030
Rank: 2
等 级:论坛游民
帖 子:36
专家分:38
注 册:2010-3-5
收藏
得分:0 
检测过了好像没出错,是不是LZ输入的时候少按了回车了啊

初学者,
2010-04-25 15:01
NOMIPS
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:86
专家分:537
注 册:2010-3-31
收藏
得分:0 
运行起来貌似没有问题。但是细节性还是要注意的。
以下是引用xiaowaibu在2010-4-25 14:16:32的发言:

输入年,月,日,;输出星期几
#include
main()
{
    int y,m,d,w;
    scanf("%d%d%d",&y,&m,&d);
    if((y%4==0&&y%100!=0)||y%400==0)
    {
        switch(m)
        {
        case 1:w=(365*y+(y/4-y/100+y/400)+d-2)%7;break;
case 2:w=(365*y+(y/4-y/100+y/400)+d+29)%7;break;
case 3:w=(365*y+(y/4-y/100+y/400)+d+58)%7;break;
case 4:w=(365*y+(y/4-y/100+y/400)+d+89)%7;break;
case 5:w=(365*y+(y/4-y/100+y/400)+d+119)%7;break;
case 6:w=(365*y+(y/4-y/100+y/400)+d+150)%7;break;
case 7:w=(365*y+(y/4-y/100+y/400)+d+180)%7;break;
case 8:w=(365*y+(y/4-y/100+y/400)+d+211)%7;break;
case 9:w=(365*y+(y/4-y/100+y/400)+d+242)%7;break;
case 10:w=(365*y+(y/4-y/100+y/400)+d+272)%7;break;
case 11:w=(365*y+(y/4-y/100+y/400)+d+303)%7;break;
case 12:w=(365*y+(y/4-y/100+y/400)+d+333)%7;break;
default: break;
        }
    }
        else
        {
        switch(m)
    {

    case 1:w=(365*y+(y/4-y/100+y/400)+d-1)%7;break;
    case 2:w=(365*y+(y/4-y/100+y/400)+d+30)%7;break;
    case 3:w=(365*y+(y/4-y/100+y/400)+d+58)%7;break;
    case 4:w=(365*y+(y/4-y/100+y/400)+d+89)%7;break;
    case 5:w=(365*y+(y/4-y/100+y/400)+d+119)%7;break;
    case 6:w=(365*y+(y/4-y/100+y/400)+d+150)%7;break;
    case 7:w=(365*y+(y/4-y/100+y/400)+d+180)%7;break;
    case 8:w=(365*y+(y/4-y/100+y/400)+d+211)%7;break;
    case 9:w=(365*y+(y/4-y/100+y/400)+d+242)%7;break;
    case 10:w=(365*y+(y/4-y/100+y/400)+d+272)%7;break;
    case 11:w=(365*y+(y/4-y/100+y/400)+d+303)%7;break;
    case 12:w=(365*y+(y/4-y/100+y/400)+d+333)%7;break;
default: break;
    }
        }
    switch(w)
    {
    case 1:printf("1\n");break;
case 2:printf("2\n");break;
case 3:printf("3\n");break;
case 4:printf("4\n");break;
case 5:printf("5\n");break;
case 6:printf("6\n");break;
case 0:printf("7\n");break;
default: break;
    }
}
2010-04-25 15:15
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
似乎没有什么问题,就是代码太复杂了.
应该可以简化一下.

建议:首先将判断是否是闰年的功能独立成一个函数;
其次定义两个枚举类来分别来记录月份和星期;
最后将计算是星期几的功能也独立成一个模块.

这样层次比较清晰.

南国利剑
2010-04-25 16:26
蓝色宁静
Rank: 1
等 级:新手上路
帖 子:17
专家分:5
注 册:2010-4-5
收藏
得分:0 
没问题,把那个 scanf("%d,%d,%d",&y,&m,&d);改成这样。再输入,能够得到结果。
2010-04-25 16:36
新学员
Rank: 1
等 级:新手上路
帖 子:2
专家分:1
注 册:2010-3-23
收藏
得分:0 
用c++6.0运行了下 都没出错啊
2010-04-25 19:43
li5683li
Rank: 2
等 级:论坛游民
帖 子:12
专家分:13
注 册:2010-4-12
收藏
得分:0 
基姆拉尔森计算公式
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
  在公式中d表示日期中的日数,m表示月份数,y表示年数。除法取整。
注意:在公式中有个与其他公式不同的地方:
 把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
2010-04-25 19:57
lihaofeng
Rank: 1
等 级:新手上路
帖 子:13
专家分:8
注 册:2010-3-10
收藏
得分:0 
那你们也帮我解释一下w=(365*y(y/4-y/100+y/400)+d-1)%7是什么意思,为啥要这样写.
2010-04-25 19:59
xiaowaibu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-3-23
收藏
得分:0 
我知道错那了,谢谢
#include <stdio.h>
main()
{
    int y,m,d,w;
    scanf("%d%d%d",&y,&m,&d);
    if((y%4==0&&y%100!=0)||y%400==0)
        switch(m)
        {
        case 1:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d)%7;break;
case 2:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+31)%7;break;
case 3:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+60)%7;break;
case 4:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+91)%7;break;
case 5:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+121)%7;break;
case 6:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+152)%7;break;
case 7:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+182)%7;break;
case 8:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+213)%7;break;
case 9:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+244)%7;break;
case 10:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+274)%7;break;
case 11:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+305)%7;break;
case 12:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+335)%7;
        }
        else
        switch(m)
    {

    case 1:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d)%7;break;
    case 2:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+31)%7;break;
    case 3:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+59)%7;break;
    case 4:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+90)%7;break;
    case 5:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+120)%7;break;
    case 6:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+151)%7;break;
    case 7:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+181)%7;break;
    case 8:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+212)%7;break;
    case 9:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+243)%7;break;
    case 10:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+273)%7;break;
    case 11:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+304)%7;break;
    case 12:w=(365*(y-1)+((y-1)/4-(y-1)/100+(y-1)/400)+d+334)%7;
    }
    switch(w)
    {
    case 1:printf("1\n");break;
case 2:printf("2\n");break;
case 3:printf("3\n");break;
case 4:printf("4\n");break;
case 5:printf("5\n");break;
case 6:printf("6\n");break;
case 0:printf("7\n");
    }
}
2010-04-30 19:30
快速回复:请高手看一下错在哪里?
数据加载中...
 
   



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

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