题目很简单,但是刚开始没想到用数组存各个月份天数,写了这堆屎。
#include <stdio.h> int Isleap(int a) { int ret=0; if (a%400==0||(a%4==0&&a%100!=0)) { ret=1; } return ret; } int main() { int year,n; scanf("%d %d",&year,&n); if (Isleap(year)) { //根据闰年与平年每个月份的天数确定区间 判断几月 if(n<32) printf("1\n%d",n); //再减去前面月份的天数 else if(n<61) printf("2\n%d",n-31); else if(n<92) printf("3\n%d",n-60); else if(n<122) printf("4\n%d",n-91); else if(n<153) printf("5\n%d",n-121); else if(n<183) printf("6\n%d",n-152); else if(n<214) printf("7\n%d",n-182); else if(n<245) printf("8\n%d",n-213); else if(n<275) printf("9\n%d",n-244); else if(n<306) printf("10\n%d",n-274); else if(n<336) printf("11\n%d",n-305); else if(n<366) printf("12\n%d",n-335); } else { if(n<32) printf("1\n%d",n); else if(n<60) printf("2\n%d",n-31); else if(n<91) printf("3\n%d",n-59); else if(n<121) printf("4\n%d",n-90); else if(n<152) printf("5\n%d",n-120); else if(n<182) printf("6\n%d",n-151); else if(n<213) printf("7\n%d",n-181); else if(n<244) printf("8\n%d",n-212); else if(n<274) printf("9\n%d",n-243); else if(n<305) printf("10\n%d",n-273); else if(n<335) printf("11\n%d",n-304); else if(n<366) printf("12\n%d",n-334); } return 0; }
用数组存各个月份天数#include <stdio.h> int Isleap(int a) { //判断是否为闰年 int ret=0; if (a%400==0||(a%4==0&&a%100!=0)) { ret=1; } return ret; } int main() { int year,n,i; scanf("%d %d",&year,&n); int leap[12]={31,29,31,30,31,30,31,31,30,31,30,31},com[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if (Isleap(year)) { //根据闰年与平年每个月份的天数确定区间 判断几月 int temp; //再减去前面月份的天数 for(i=0;n>0;i++) { temp=n; n-=leap[i]; } printf("%d\n%d",i,temp); } else { int temp; //temp记录上一个n的值,当n小于0结束循环,不用temp记录会打印“3\n -2”情况 for(i=0;n>0;i++) { temp=n; n-=com[i]; } printf("%d\n%d",i,temp); } return 0; }