• ccf-201503-3(细节题)(90分)


    问题描述

    试题编号: 201503-3
    试题名称: 节日
    时间限制: 1.0s
    内存限制: 256.0MB
    问题描述:
    问题描述
      有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日。
      现在,给你abcy1y2(1850 ≤ y1y2 ≤ 2050),希望你输出从公元y1年到公元y2年间的每年的a月的第b个星期c的日期。
      提示:关于闰年的规则:年份是400的整数倍时是闰年,否则年份是4的倍数并且不是100的倍数时是闰年,其他年份都不是闰年。例如1900年就不是闰年,而2000年是闰年。
      为了方便你推算,已知1850年1月1日是星期二。
    输入格式
      输入包含恰好一行,有五个整数abcy1y2。其中c=1, 2, ……, 6, 7分别表示星期一、二、……、六、日。
    输出格式
      对于y1y2之间的每一个年份,包括y1y2,按照年份从小到大的顺序输出一行。
      如果该年的a月第b个星期c确实存在,则以"yyyy/mm/dd"的格式输出,即输出四位数的年份,两位数的月份,两位数的日期,中间用斜杠“/”分隔,位数不足时前补零。
      如果该年的a月第b个星期c并不存在,则输出"none"(不包含双引号)。
    样例输入
    5 2 7 2014 2015
    样例输出
    2014/05/11
    2015/05/10
    评测用例规模与约定
      所有评测用例都满足:1 ≤ a ≤ 12,1 ≤ b ≤ 5,1 ≤ c ≤ 7,1850 ≤ y1y2 ≤ 2050。

    此题的细节比较多,要面面考虑,不知道哪里还有什么坑,还差十分

    代码如下:

    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    using namespace std; 
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    int a,b,c,y1,y2,day;
    long long sum;
    
    int judge(int x)
    {
    	if(x%4==0&&x%100!=0||x%400==0)
    		return 1;
    	return  0;
    }
    
    int mouth_judge(int x,int year)
    {
    	if(x==0)	return 0;
    	else if(x==1)	return 31;
    	else if(x==2)	return 31+28+judge(year);
    	else if(x==3)	return 31+28+31+judge(year);
    	else if(x==4)	return 31+28+31+30+judge(year);
    	else if(x==5)	return 31+28+31+30+31+judge(year);
    	else if(x==6)	return 31+28+31+30+31+30+judge(year);
    	else if(x==7)	return 31+28+31+30+31+30+31+judge(year);
    	else if(x==8)	return 31+28+31+30+31+30+31+31+judge(year);
    	else if(x==9)	return 31+28+31+30+31+30+31+31+30+judge(year);
    	else if(x==10)	return 31+28+31+30+31+30+31+31+30+31+judge(year);
    	else if(x==11)	return 31+28+31+30+31+30+31+31+30+31+30+judge(year);
    }
    
    int is_right(int mouth,int day,int year)
    {
    	if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12)
    		if(day>31)
    		{
    			return 0;
    		}
    	else if(mouth==2)
    	{
    		if(day>28+judge(year))
    		return 0;
    	}
    	else 
    	{
    		if(day>30)
    		return 0;
    	}
    	return 1;
    			
    }
    
    void print(int year,int mou,int day)
    {
    	cout<<year<<'/';
    	if(a<10)
    		cout<<'0';
    	cout<<mou<<'/';
    	if(day<10)
    		cout<<'0';
    	cout<<day<<endl;
    }
    
    int main(int argc, char** argv) {
    	freopen("in.txt","r",stdin);
    	cin>>a>>b>>c>>y1>>y2;
    	for(int i=1850;i<y1;i++)
    	{
    		sum=sum+judge(i)+365;
    	}
    	sum+=2;		//月初第一天 
    	long long sum1=sum; 
    	for(int i=y1;i<=y2;i++)
    	{
    		day=0;
    		sum1=sum+mouth_judge(a-1,i);
    		sum1=sum1%7;
    		if(sum1==0)
    			sum1=7;
    		if(sum1>c)
    		{
    			day=(7-sum1)+(b-1)*7+1+c;
    		}
    		else
    		{
    			if(b==1)
    			{
    				day=c-sum1+1;
    			}
    			else
    			{
    				day=(7-sum1)+(b-2)*7+c+1;
    			}
    		}
    		sum=sum+judge(i)+365;
    		if(is_right(a,day,i))
    			print(i,a,day);
    		else cout<<"none"<<endl;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    React Native组件之Text
    React Native的组件ListView
    React Native中的网络请求fetch和简单封装
    解决FileUpload上传大文件报错
    以EF形式操作mysql数据库
    以ADO形式操作mysql数据库
    Docker
    Docker创建镜像文件并在容器中运行
    WIN10下的Docker安装
    MySQL安装
  • 原文地址:https://www.cnblogs.com/muziqiu/p/10523775.html
Copyright © 2020-2023  润新知