文章

4

粉丝

135

获赞

11

访问

22.5k

头像
日期(1011)题解
P1011 贵州大学机试题
发布于2022年8月1日 16:10
阅读数 8.0k

思路分析

首先,计算输入的日期距离4月12日的天数;

然后,将天数+3对7取余(对7取余的原因是一周有7天);

最后,根据第二步的计算结果,输出数组中对应的周几的名称。

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

struct node{
	int year, month, day;
}p;
int f[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string s[7]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

int main()
{
	int ans = 0;
	cin>>p.month>>p.day;
	if (p.month==4){
		ans = p.day - 12;
	}
	else if (p.month>=5 && p.month<13){
		for (int i=5; i<p.month; i++){
			ans += f[i];
		}
		ans = ans + 18 + p.day;
	}

	int res = (ans+3)%7;
	cout<<s[res]<<endl;
	
	return 0;
 } 

 

登录查看完整内容


登录后发布评论

1 条评论
月溅星河
2023年12月15日 16:31

赞一个

赞(1)