[目前写的最长的代码]语法入门班选择结构习题----Q-前天是哪天

前天是哪天

https://ac.nowcoder.com/acm/contest/19304/Q

题目描述

给定公元2000年到公元3000年之间的某一天,请你给出该天的前天是哪一天.

输入描述:

输入在一个日期,格式如"yyyy-mm-dd",题目保证所有输入日期为合法日期。

输出描述:

在一行中输出日期,格式如"yyyy-mm-dd"。

示例1

输入

2020-11-15

输出

2020-11-13

备注:

注意日期格式,月份或者天数不足2位要补零。

解法:

题目看起来不难,就是在几个节点需要注意分情况讨论:

1、1月1、2号的时候前天是前年

2、判断年份是否是闰年,因为2月到底有几天关系着3月1、2号的前天是几号

3、7、8月份都有31天

int main(){
    int year, mon, date ;
    scanf("%4d-%02d-%02d", &year,&mon,&date);
    if(year%4==0&&year%100!=0||year%400==0){
        if(mon==3){
        if(date==2)
            printf("%4d-02-29",year);
        else{
             if(date==1)
                printf("%4d-02-28",year);
            else
                printf("%4d-%02d-%02d",year,mon,date-2);
             }
        }
        
            else{
                if(mon==1){
                    if(date==1)
                        printf("%4d-12-30",year-1);
                    else{
                        if(date==2)
                            printf("%4d-12-31",year-1);
                        else
                            printf("%4d-%02d-%02d",year,mon,date-2);
                    }
                }
                
                else{
                         if(mon==4||mon==6||mon==9||mon==11){
                        if(date==1)
                        printf("%4d-%02d-30",year,mon-1);
                   else{
                       if(date==2)
                        printf("%4d-%02d-31",year,mon-1);
                    else
                    printf("%4d-%02d-%02d",year,mon,date-2);
                   }
                }
                
               else{
                   if(mon==8){
                         if(date==1)
                         printf("%4d-%02d-30",year,mon-1);
                   else{
                       if(date==2)
                        printf("%4d-%02d-31",year,mon-1);
                       else
                    printf("%4d-%02d-%02d",year,mon,date-2);
                       }
                                 }
                else{
                    if(date==1)
                         printf("%4d-%02d-29",year,mon-1);
                   else{
                       if(date==2)
                        printf("%4d-%02d-30",year,mon-1);
                       else
                    printf("%4d-%02d-%02d",year,mon,date-2);
                   }
                }
                    
                    
            }
        
                }
    }
                                        }
    
        else{
              if(mon==1){
                    if(date==1)
                        printf("%4d-12-30",year-1);
                    else{
                        if(date==2)
                            printf("%4d-12-31",year-1);
                        else
                            printf("%4d-%02d-%02d",year,mon,date-2);
                    }
                }
             else{
                 if(mon==3){
                 if(date==2)
                   printf("%4d-02-28",year);
                 else{
                     if(date==1)
                printf("%4d-02-27",year);
                       else
                printf("%4d-%02d-%02d",year,mon,date-2);
             }
        }
             
            else{
                if(mon==4||mon==6||mon==9||mon==11){
                    
                    if(date==1)
                        printf("%4d-%02d-30",year,mon-1);
                   else{
                       if(date==2)
                        printf("%4d-%02d-31",year,mon-1);
                    else
                    printf("%4d-%02d-%02d",year,mon,date-2);
                   }
                }
                
                else{
                    if(mon==8){
                         if(date==1)
                         printf("%4d-%02d-30",year,mon-1);
                   else{
                       if(date==2)
                        printf("%4d-%02d-31",year,mon-1);
                       else
                    printf("%4d-%02d-%02d",year,mon,date-2);
                       }
                                 }
                 else{
                     if(date==1)
                         printf("%4d-%02d-29",year,mon-1);
                   else{
                       if(date==2)
                        printf("%4d-%02d-30",year,mon-1);
                       else
                    printf("%4d-%02d-%02d",year,mon,date-2);
                   }
                }
                    
                    
            }
             }
    }
        }
        return 0;
}
全部评论

相关推荐

3 收藏 评论
分享
牛客网
牛客企业服务