【日期计算】一个可以死算出来的题目/数学规律和死算你选一个就行了
死算出来了所有月份的范围,然后计算d和每个月份开始的差值就可以了:
闰年:
The number 1 month is from 1 to 31
The number 2 month is from 32 to 59
The number 3 month is from 60 to 90
The number 4 month is from 91 to 120
The number 5 month is from 121 to 151
The number 6 month is from 152 to 181
The number 7 month is from 182 to 212
The number 8 month is from 213 to 243
The number 9 month is from 244 to 273
The number 10 month is from 274 to 304
The number 11 month is from 305 to 334
The number 12 month is from 335 to 365
非闰年:
The number 1 month is from 1 to 31
The number 2 month is from 32 to 60
The number 3 month is from 61 to 91
The number 4 month is from 92 to 121
The number 5 month is from 122 to 152
The number 6 month is from 153 to 182
The number 7 month is from 183 to 213
The number 8 month is from 214 to 244
The number 9 month is from 245 to 274
The number 10 month is from 275 to 305
The number 11 month is from 306 to 335
The number 12 month is from 336 to 366
再加上闰年判断即可;
代码如下:
include
bool is29(int y){
if(y%4 == 0&&y%100!=0){
return true;
}else if(y%400==0){
return true;
}
return false;
}
using namespace std;
int main(){
int y,d;
cin >> y >> d;
bool isRun = is29(y);
if(isRun){
if(d <= 31){
printf("1\n%d",d);
}else if(d <= 60){
printf("2\n%d",d-31);
}else if(d <= 91){
printf("3\n%d",d-60);
}else if(d <= 121){
printf("4\n%d",d-91);
}else if(d <=152){
printf("5\n%d",d-121);
}else if(d <=182){
printf("6\n%d",d-152);
}else if(d <=213){
printf("7\n%d",d-182);
}else if(d <=244){
printf("8\n%d",d-213);
}else if(d <=274){
printf("9\n%d",d-244);
}else if(d <=305){
printf("10\n%d",d-274);
}else if(d <=335){
printf("11\n%d",d-305);
}else if(d <=366){
printf("12\n%d",d-335);
}
}else {
if(d <= 31){
printf("1\n%d",d);
}else if(d <= 59){
printf("2\n%d",d-31);
}else if(d <= 90){
printf("3\n%d",d-59);
}else if(d <= 120){
printf("4\n%d",d-90);
}else if(d <=151){
printf("5\n%d",d-120);
}else if(d <=181){
printf("6\n%d",d-151);
}else if(d <=212){
printf("7\n%d",d-181);
}else if(d <=243){
printf("8\n%d",d-212);
}else if(d <=273){
printf("9\n%d",d-243);
}else if(d <=304){
printf("10\n%d",d-273);
}else if(d <=334){
printf("11\n%d",d-304);
}else if(d <=365){
printf("12\n%d",d-334);
}
}
}

浙公网安备 33010602011771号