• [刷题] PTA 闰年


    PTA

     1 #include<stdio.h>
     2 int main() {
     3     int year,month,day,cnt,flag;
     4     flag = 0;
     5     scanf("%4d/%2d/%2d",&year,&month,&day);
     6     if((year%4 ==0 && year%100 != 0) || (year%400 == 0))
     7         flag = 1;
     8     if(month == 1)
     9         cnt = day;
    10     if(month == 2)
    11         cnt = 31 + day;
    12     if(month == 3)
    13         cnt = 31 + 28 + day + flag;
    14     if(month == 4)
    15         cnt = 31 + 28 + 31 + day + flag;
    16     if(month == 5)
    17         cnt = 31 + 28 + 31 + 30 + day + flag;
    18     if(month == 6)
    19         cnt = 31 + 28 + 31 + 30 + 31 + day + flag;
    20     if(month == 7)
    21         cnt = 31 + 28 + 31 + 30 + 31 + 30 + day + flag;
    22     if(month == 8)
    23         cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day + flag;
    24     if(month == 9)
    25         cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day + flag;
    26     if(month == 10)
    27         cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day + flag;
    28     if(month == 11)
    29         cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day + flag;
    30     if(month == 12)
    31         cnt = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day + flag;
    32     printf("%d",cnt);
    33 }

    简便的写法,用了数组和循环:

     1 #include<stdio.h>
     2 int main() {
     3     int year,month,day,cnt,flag,i;
     4     int a[] = {31,28,31,30,31,30,31,31,30,31,30,31};
     5     flag = 0;
     6     scanf("%4d/%2d/%2d",&year,&month,&day);
     7     if((year%4 ==0 && year%100 != 0) || (year%400 == 0))
     8         flag = 1;
     9     for(i = 1; i < month; i++)
    10         cnt = cnt + a[i];
    11     cnt = cnt + day;
    12     if(month > 2 && flag ==1)
    13         cnt++;
    14     printf("%d",cnt);
    15 }
  • 相关阅读:
    Word自带的文献管理功能的具体实现步骤
    线程通信中的细节问题
    Java中static方法、程序入口函数main方法的继承问题
    Android中模拟器启动中出现“emulator-arm.exe已停止工作”
    CMD命令详解
    jQuery实现回到顶部功能
    Toad&PL/SQL修改查询信息
    TOAD常用快捷键
    ORACLE WITH AS 用法
    常用快捷键(转)
  • 原文地址:https://www.cnblogs.com/cxc1357/p/10644972.html
Copyright © 2020-2023  润新知