| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 579 人关注过本帖
标题:输入日期,求星期,帮我查一下哪里错了,求助···
只看楼主 加入收藏
zhy7723998
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
输入日期,求星期,帮我查一下哪里错了,求助···
题目描述
A year and the day of week of the first day in this year will be given to you, you are asked to calculate the day of week of any given date for this year.
输入
The input may contain several test cases.
The first line of each test case is a year (a 4-digit positive integer) and the day of week of the first day in this year (Sun, Mon, Tue, Wed, Thu, Fri, or Sat).
The second line is the given date of this year (in format of integer/integer, denoting Month/Day). You need to calculate the day of week of this date.
Input is terminated by EOF.
输出
For each test case, output the day of week of the given date in this year (i.e. Sun, Mon, Tue, Wed, Thu, Fri, or Sat)
样例输入
2009 Thu
12/25
1996 Mon
12/25
1984 Sun
2/2
样例输出
Fri
Wed
Thu

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int y;
    string w;
    while(cin >> y >> w)
    {
        int m,d,n,W;
        if(w=="Sun")  W=0;
        else if(w=="Mom")  W=1;
        else if(w=="Tue")  W=2;
        else if(w=="Wed")  W=3;
        else if(w=="Thu")  W=4;
        else if(w=="Fri")  W=5;
        else if(w=="Sat")  W=6;
        scanf("%d/%d",&m,&d);
        if(m==1)  n=d%7;
        else if (m==2)  n=(d+31)%7;
        else
        {
            if ((y%4==0&&y%100!=0)||y%400==0)
            {
                if(m==3)  n=(d+31+29)%7;
                if(m==4)  n=(d+31+29+31)%7;
                if(m==5)  n=(d+31+29+31+30)%7;
                if(m==6)  n=(d+31+29+31+30+31)%7;
                if(m==7)  n=(d+31+29+31+30+31+30)%7;
                if(m==8)  n=(d+31+29+31+30+31+30+31)%7;
                if(m==9)  n=(d+31+29+31+30+31+30+31+31)%7;
                if(m==10)  n=(d+31+29+31+30+31+30+31+31+30)%7;
                if(m==11)  n=(d+31+29+31+30+31+30+31+31+30+31)%7;
                if(m==12)  n=(d+31+29+31+30+31+30+31+31+30+31+30)%7;
            }
            else
            {
                if(m==3)  n=(d+31+28)%7;
                if(m==4)  n=(d+31+28+31)%7;
                if(m==5)  n=(d+31+28+31+30)%7;
                if(m==6)  n=(d+31+28+31+30+31)%7;
                if(m==7)  n=(d+31+28+31+30+31+30)%7;
                if(m==8)  n=(d+31+28+31+30+31+30+31)%7;
                if(m==9)  n=(d+31+28+31+30+31+30+31+31)%7;
                if(m==10)  n=(d+31+28+31+30+31+30+31+31+30)%7;
                if(m==11)  n=(d+31+28+31+30+31+30+31+31+30+31)%7;
                if(m==12)  n=(d+31+28+31+30+31+30+31+31+30+31+30)%7;
            }
        }
        W=W+n;
        if(W>7)  W=W%7;
        else if(W==0)  cout << "Sun\n";
        else if(W==1)  cout << "Mom\n";
        else if(W==2)  cout << "Tue\n";
        else if(W==3)  cout << "Wed\n";
        else if(W==4)  cout << "Thu\n";
        else if(W==5)  cout << "Fri\n";
        else if(W==6)  cout << "Sat\n";
    }
    return 0;
}
搜索更多相关主题的帖子: positive contain several second 
2015-04-17 14:17
Vsnow
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:124
专家分:145
注 册:2015-1-3
收藏
得分:10 
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int year,month,day;
    puts("请输入年份:");    cin>>year;
    puts("请输入月份:");    cin>>month;
    puts("请输入日期:");    cin>>day;
    int t,days,m;
    if((year%4==0)||((year%4==0)&&(year%100==0)))
        t=1;
    else    t=0;
    if(t==1)
    {
        switch(month)
        {
        case 1: days=day;
            m=day%7;
        case 2: days=31+day;
            m=days%7;
        case 3: days=28+31+day;
            m=day%7;
        case 4: days=31+28+31+day;
            m=day%7;
        case 5: days=30+31+28+31+day;
            m=day%7;
        case 6: days=31+30+31+28+31+day;
            m=day%7;
        case 7: days=30+31+30+31+28+31+day;
            m=day%7;
        case 8: days=31+30+31+30+31+28+31+day;
            m=day%7;
        case 9: days=31+31+30+31+30+31+28+31+day;
            m=day%7;
        case 10: days=30+31+31+30+31+30+31+28+31+day;
            m=day%7;
        case 11: days=31+30+31+31+30+31+30+31+28+31+day;
            m=day%7;
        case 12: days=30+31+30+31+31+30+31+30+31+28+31+day;
            m=day%7;
        }
    }
    else if(t==0)
    {
        switch(month)
        {
        case 1: days=day;
            m=day%7;
        case 2: days=31+day;
            m=days%7;
        case 3: days=29+31+day;
            m=day%7;
        case 4: days=31+29+31+day;
            m=day%7;
        case 5: days=30+31+29+31+day;
            m=day%7;
        case 6: days=31+30+31+29+31+day;
            m=day%7;
        case 7: days=30+31+30+31+29+31+day;
            m=day%7;
        case 8: days=31+30+31+30+31+29+31+day;
            m=day%7;
        case 9: days=31+31+30+31+30+31+29+31+day;
            m=day%7;
        case 10: days=30+31+31+30+31+30+31+29+31+day;
            m=day%7;
        case 11: days=31+30+31+31+30+31+30+31+29+31+day;
            m=day%7;
        case 12: days=30+31+30+31+31+30+31+30+31+29+31+day;
            m=day%7;
        }
    }
    switch(m)
    {
   
        case 0:    puts("星期天");    break;
        case 1:    puts("星期一");    break;
        case 2:    puts("星期二");    break;
        case 3:    puts("星期三");    break;
        case 4:    puts("星期四");    break;
        case 5:    puts("星期五");    break;
        case 6:    puts("星期天");    break;
    }
    return 0;
}
2015-04-19 15:45
lzlz1111
Rank: 2
等 级:论坛游民
帖 子:19
专家分:54
注 册:2015-3-13
收藏
得分:10 
1. scanf("%d %d",&m,&d);
2. else if(w=="Mom")  W=1;  ==>  else if(w=="Mon")  W=1;
3. 1984 Sun, 2/2  ==>  should be Fir
2015-04-19 16:14
快速回复:输入日期,求星期,帮我查一下哪里错了,求助···
数据加载中...
 
   



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

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