题解 | #打印日期#

打印日期

https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b

#include <stdio.h>
void inttostring(int temp,char a[2]){
    int t1=temp/10;
    int t2=temp%10;
    char a0='0';
    a[0]=(char)(t1+(int)a0);
    a[1]=(char)(t2+(int)a0);
}
int main() {
    int a,b;
    while (scanf("%d %d", &a, &b) != EOF) { // 注意 while 处理多个 case
        char temp1[2]={0},temp2[2]={0};int temp=0;int sum=0;
        if(a%4==0&&a%100!=0||a%400==0){
            if(0<b&&b<=31){
                sum=0;
                temp1[0]='0';
                temp1[1]='1';
                temp=b-sum;
                inttostring(temp, temp2);
            }else if(31<b&&b<=60){
                sum=31;
                temp1[0]='0';
                temp1[1]='2';
                temp=b-sum;
                
                inttostring(temp, temp2);
            }else if(60<b&&b<=91){
                sum=60;
                temp1[0]='0';
                temp1[1]='3';
                temp=b-sum;
                
                inttostring(temp, temp2);
            }else if(91<b&&b<=121){
                sum=91;
                temp1[0]='0';
                temp1[1]='4';
                
                inttostring(temp, temp2);
            }else if(121<b&&b<=152){
                sum=121;
                temp1[0]='0';
                temp1[1]='5';
                temp=b-sum;
                
                inttostring(temp, temp2);
            }else if(152<b&&b<=182){
                sum=152;
                temp1[0]='0';
                temp1[1]='6';
                temp=b-sum;
                
                inttostring(temp, temp2);
            }else if(182<b&&b<=213){
                sum=182;
                temp1[0]='0';
                temp1[1]='7';
                temp=b-sum;
                inttostring(temp, temp2);
            }else if(213<b&&b<=244){
                sum=213;
                temp1[0]='0';
                temp1[1]='8';
                temp=b-sum;
                
                inttostring(temp, temp2);
            }else if(244<b&&b<=274){
                sum=244;
                temp1[0]='0';
                temp1[1]='9';
                temp=b-sum;
                
                inttostring(temp, temp2);
            }else if(274<b&&b<=305){
                sum=274;
                temp1[0]='1';
                temp1[1]='0';
                temp=b-sum;
                sum=305;
                inttostring(temp, temp2);
            }else if(305<b&&b<=335){
                sum=305;
                temp1[0]='1';
                temp1[1]='1';
                temp=b-sum;
                sum=335;
                inttostring(temp, temp2);
            }else if(335<b&&b<=366){
                sum=335;
                temp1[0]='1';
                temp1[1]='2';
                temp=b-sum;
                sum=366;
                inttostring(temp, temp2);
            }
        }else {
            sum=0;
            if(0<b&&b<=31){
                sum=0;
                temp1[0]='0';
                temp1[1]='1';
                temp=b-sum;
                sum+=31;
                inttostring(temp, temp2);
            }else if(31<b&&b<=60-1){
                sum=31;
                temp1[0]='0';
                temp1[1]='2';
                temp=b-sum;
                sum=60-1;
                inttostring(temp, temp2);
            }else if(60-1<b&&b<=91-1){
                sum=60-1;
                temp1[0]='0';
                temp1[1]='3';
                temp=b-sum;
                sum=91-1;
                inttostring(temp, temp2);
            }else if(91-1<b&&b<=121-1){
                sum=91-1;
                temp1[0]='0';
                temp1[1]='4';
                temp=b-sum;
                sum=121-1;
                inttostring(temp, temp2);
            }else if(121-1<b&&b<=152-1){
                sum=121-1;
                temp1[0]='0';
                temp1[1]='5';
                temp=b-sum;
                sum=152-1;
                inttostring(temp, temp2);
            }else if(152-1<b&&b<=182-1){
                sum=152-1;
                temp1[0]='0';
                temp1[1]='6';
                temp=b-sum;
                sum=182-1;
                inttostring(temp, temp2);
            }else if(182-1<b&&b<=213-1){
                sum=182-1;
                temp1[0]='0';
                temp1[1]='7';
                temp=b-sum;
                sum=213-1;
                inttostring(temp, temp2);
            }else if(213-1<b&&b<=244-1){
                sum=213-1;
                temp1[0]='0';
                temp1[1]='8';
                temp=b-sum;
                sum=244-1;
                inttostring(temp, temp2);
            }else if(244-1<b&&b<=274-1){
                sum=244-1;
                temp1[0]='0';
                temp1[1]='9';
                temp=b-sum;
                sum=274-1;
                inttostring(temp, temp2);
            }else if(274-1<b&&b<=305-1){
                sum=274-1;
                temp1[0]='1';
                temp1[1]='0';
                temp=b-sum;
                sum=305-1;
                inttostring(temp, temp2);
            }else if(305-1<b&&b<=335-1){
                sum=305-1;
                temp1[0]='1';
                temp1[1]='1';
                temp=b-sum;
                sum=335-1;
                inttostring(temp, temp2);
            }else if(335-1<b&&b<=366-1){
                sum=335-1;
                temp1[0]='1';
                temp1[1]='2';
                temp=b-sum;
                sum=366-1;
                inttostring(temp, temp2);
            }
        }
        printf("%d-%c%c-%c%c\n",a,temp1[0],temp1[1],temp2[0],temp2[1]);
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
有趣的牛油果开挂了:最近这个阶段收到些杂七杂八的短信是真的烦
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务