NC23897. Mother's Day
描述
输入描述
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");}}}