列表

详情


NC23897. Mother's Day

描述

Mother's Day is a celebration honoring the mother of the family, as well as motherhood, maternal bonds, and the influence of mothers in society, which is celebrated on various days in many parts of the world, but mostly, the date is on the second Sunday in May. 
    Like Mother's Day, there are also several celebrations have no fixed date, but are set in the form of "the Bth(st/nd) weekday C of the Ath(st/nd) month in a year", for example, as above, Mother's Day is on the second Sunday in May of a year, but also can be described as "the 2nd weekday 7 of the 5st month in a year"(A=5, B=2, C=7). 
    Now, to help remember these special days, HVT wants you to calculate all the exact date of such celebrations in the past and future hundreds of years, which means that you are given 4 numbers: A(1 ≤ A ≤ 12), B(B ≥ 1), C(1 ≤ C ≤ 7, 1=Monday,2=Tuesday,...,7=Sunday) and y(represents the year, 1850 ≤ y ≤ 2050), and you should write code to calculate the exact date of "the Bth(st/nd) weekday C of the Ath(st/nd) month in a year y".
    For your convenience, we will noice you that: January 1st, 1850 is a Tuesday.

输入描述

The input contains multiple lines of test data, each line has 4 numbers: A B C and y.

输出描述

For each input, you should output a exact date in the format of "yyyy/mm/dd"(When the number of digits is insufficient, 0 should be added to the front);
if the Bth(st/nd) weekday C of the Ath(st/nd) month in a year y does not exist(for example, there will never be a 7th Monday in any months of any year), you should output "none" (without quotes).

示例1

输入:

4 2 7 2018
4 1 7 2018
2 5 4 2018
2 4 3 2018

输出:

2018/04/08
2018/04/01
none
2018/02/28

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 360K, 提交时间: 2020-02-27 00:24:23

#include<bits/stdc++.h>
#define ll long long
#define fo(i,j,n) for(register int i=j;i<=n;++i)
using namespace std;
int A,B,C,D;
bool ruan(int x)
{
	if((x%4==0&&x%100!=0)||x%400==0) return 1;
	return 0;
}
int main()
{
	while(cin>>A>>B>>C>>D)
	{
		int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
		if(ruan(D)) m[2]++;
		int d=2;
		for(int i=1850;i<D;i++)
		{
			if(ruan(i)) d+=366;
			else d+=365;
			d%=7;
		}
		for(int i=1;i<A;i++)
		{
			d+=m[i];
			d%=7;
		}
		if(d==0) d=7;
		int base=7*(B-1)+C;
		if(d<=C) base-=(d-1);
		else if(d>C) base+=(7-C);
		if(base>m[A]) puts("none");
		else printf("%d/%02d/%02d\n",D,A,base);
	 } 
	 return  0;
}

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 376K, 提交时间: 2020-05-29 22:39:31

#include<stdio.h>
int o[13]={0,31,28,31,30,31,30,31,31,30,31,30,31},d,y,n,m,s,i,r;main(){while(scanf("%d%d%d%d",&m,&n,&d,&y)!=EOF){s=r=0;s+=365*(y-1850)+(y/4-1850/4);if(y>=1900){s--;}if(y%400==0||(y%4==0&&y%100!=0)){o[2]=29;}else{o[2]=28;}for(i=1;i<=m-1;i++){s+=o[i];}s=(s+2)%7;if(s==0){s=7;}r=1+7*(n-1);if(d>=s){r+=d-s;}else{r+=d+7-s;}if(r<=o[m]){printf("%d/%02d/%02d\n",y,m,r);}else{puts("none");}}}

上一题