CCF日期计算,解题思路,C++,Java

解题思路:

首先判断是闰年还是平年,若是瑞年则28+1,平年28+0

bool flag = n%4 ==0&& n%100 != 0 || n%400 == 0 ;
	if(flag){
		t=1;
	}else{
		t=0;
	}


#include <iostream>
using namespace std;
int main(){
	int n,d,t;

	cin>>n>>d;
	if(d <=31){
		cout<<1<<endl;
		cout<<d;
		return 0;
	}
	bool flag = n%4 ==0&& n%100 != 0 || n%400 == 0 ;
	if(flag){
		t=1;
	}else{
		t=0;
	}
	if( d <=59+t){// 31+28 + t 如果t=1 则 d< 60 即2月有29天,若是0,则2月有28天 
		cout<<2<<endl;
		cout<< d-31;
	}else if( d <= 90+t){ // 31 + 2月(28 + t)+ 31 = 90+t  如果t=1 则 d< 60 即2月有29天,若是0,则2月有28天 ,下面的情况类似 
		cout<<3 <<endl;
		cout<< d-(59+t);
	}else if( d <= 120+t){
		cout<<4<<endl;
		cout<< d-(90+t);
	}else if( d <= 151+t){
		cout<<5 <<endl;
		cout<< d-120-t;
	}else if( d <= 181+t){
		cout<<6 <<endl;
		cout<< d-151-t;
	}else if( d <= 212+t){
		cout<<7<<endl;
		cout<< d-181-t;	
	}else if( d <= 243+t){
		cout<<8 <<endl;
		cout<< d-212-t;
	}else if( d <= 273+t){
		cout<<9 <<endl;
		cout<< d-243-t;
	}else if( d <= 304+t){
		cout<<10 <<endl;
		cout<< d-273-t;
	}else if( d <= 334+t){
		cout<<11 <<endl;
		cout<< d-304-t;
	}else if( d <= 365+t){
		cout<<12 <<endl;
		cout<< d-334-t;		
	}
	return 0;
} 


猜你喜欢

转载自blog.csdn.net/song91425/article/details/78675039