这也是一道经典的算法题。
其实也是用两个数组。还有判断是否闰年。
两个个循环,外面一个是月份循环,内部一个是每个月的天数循环,然后计数器Count++就行,直到和天数相同就跳出循环,打印就行。
#include <stdio.h>
int judge ( int year ) {
if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0 ) {
return 1 ;
}
return 0 ;
}
int main ( ) {
int year , days ;
int A[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int B[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
while( scanf("%d %d",&year,&days) != EOF ) {
int i = 1 , tem = 0 ;
int count = 0 ;
if( judge(year) == 1 ) {
for ( ; i <= 12 ; i++ ) {
for( int j = 1 ; j <= B[i] ; j++ ) {
count++;
if( count == days ) {
tem = j ;
break ;
}
}
if( count == days ) {
break ;
}
}
}else{
for ( ; i <= 12 ; i++ ) {
for( int j = 1 ; j <= A[i] ; j++ ) {
count++;
if( count == days ) {
tem = j ;
break ;
}
}
if( count == days ) {
break ;
}
}
}
if( i < 10 ) {
if ( tem < 10 ) {
printf( "%d-0%d-0%d\n",year,i,tem ) ;
}else {
printf( "%d-0%d-%d\n",year,i,tem ) ;
}
}else{
if ( tem < 10 ) {
printf( "%d-%d-0%d\n",year,i,tem ) ;
}else {
printf( "%d-%d-%d\n",year,i,tem ) ;
}
}
}
return 0 ;
}
结果如下:
标签:打印,int,31,30,year,算法,循环,经典 From: https://www.cnblogs.com/llllmz/p/17948219