哈理工OJ 1575 Calculate the day-1(水模拟)

本文介绍了一个简单的算法,用于计算从当前指定为某个星期几的9月1日起,经过若干年后9月1日会是星期几。通过输入当前9月1日所在的星期和年数,程序将输出指定年份9月1日对应的星期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1575

Calculate the day-1
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 123(64 users) Total Accepted: 90(62 users) Rating: Special Judge: No
Description
September 1st is a boring day, because this day is the beginning of the new semester. But we are curious about what day is September 1st in a week every year. Given the hypothesis of September 1st this year which suppose to be x(x belong to Monday, Tuesday and so on) in a week.Please calculate what September 1st is after n (n is an integer)years.

Input
Input a series of test date.

Each line including a string x and an integer n.(n<100)

Output
Output a string, said what September 1st is after n years.

Sample Input
Saturday 12

Monday 10

Sample Output
Sunday

Saturday

Hint
Assuming this year is a leap year.
Author
孟祥凤@Amber

【思路分析】题目给说了n<100,所以难度降低了不少。然后一开始的时候是闰年,那么只考虑四年一闰就好了,暴力扫一遍,看到n年之后的9月1号总共过了多少天,然后根据现在的9月1号是星期几求n年之后的9月1号是星期几。
【AC代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char a[7][10]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
char str[10];
int main()
{
    int n;
    while(~scanf("%s %d",str,&n))
    {
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            if(i%4==0)
            {
                sum+=366;
            }
            else
            {
                sum+=365;
            }
        }
        for(int i=0; i<7; i++)
        {
            if(strcmp(a[i],str)==0)
            {
                sum+=(i+1);
                break;
            }
        }
        if(sum%7==0)
        {
            printf("Sunday\n");
        }
        else
        {
            printf("%s\n",a[sum%7-1]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值