• 2014年第五届蓝桥杯C/C++ A组国赛 —— 第三题:日期差


    标题:日期差

    历史上,不同的人类聚居地可能有不同的历法,因而记录下来的资料中日期的换算就很麻烦。幸好今天我们统一使用公元纪年法。当然,这种历法对求两个日期差多少天也不是十分简便,但毕竟是可以忍受的。

    下面的程序计算了两个日期的差值,两个日期都使用公元纪年法。

    请分析程序逻辑,并推断划线部分缺失的代码。

    int to_day(int y, int m, int d)
    {
    	int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    	int day = 0;
    	int i;
    	for(i=1; i<y; i++){
    		day += (i%4==0 && i%100!=0 || i%400==0)? 366 : 365;
    	}
    	
    	if(y%4==0 && y%100!=0 || y%400==0) mon[2]++;
    	
    	for(i=1; i<m; i++){
    		_____________________;  //填空位置
    	}
    	
    	return day + d;
    }
    
    int diff(int y1, int m1, int d1, int y2, int m2, int d2)
    {
    	int a = to_day(y1, m1, d1);
    	int b = to_day(y2, m2, d2);
    	return b-a;
    }
    
    int main()
    {
    	int n = diff(1864,12,31,1865,1,1);
    	printf("%d
    ", n);
    	return 0;
    }
    
    

    注意:通过浏览器提交答案。只填写缺少的内容,不要填写任何多余的内容(例如:说明性文字或已有符号)。

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
     Determination algorithm:
    
     Determining data structure:
    
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <cstdio>
    
    int to_day(int y, int m, int d)
    {
    	int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    	int day = 0;
    	int i;
    	for(i=1; i<y; i++){
    		day += (i%4==0 && i%100!=0 || i%400==0)? 366 : 365;
    	}
    	
    	if(y%4==0 && y%100!=0 || y%400==0) mon[2]++;
    	
    	for(i=1; i<m; i++){
    		day += mon[i];  //填空位置
    	}
    	
    	return day + d;
    }
    
    int diff(int y1, int m1, int d1, int y2, int m2, int d2)
    {
    	int a = to_day(y1, m1, d1);
    	int b = to_day(y2, m2, d2);
    	return b-a;
    }
    
    int main()
    {
    	int n = diff(1864,12,31,1865,1,1);
    	printf("%d
    ", n);
    	return 0;
    }
    
    
  • 相关阅读:
    单例/单体模式(Singleton)
    步步为营 .NET 设计模式学习笔记 六、Adapter(适配器模式)
    分享18个非常棒的 jQuery 表格插件
    线程池 vs 专有线程
    介绍27款经典的CSS框架
    一些系统开发及项目管理的资料
    SQL数据库中的XML应用
    SQLite之初体验
    AsyncBox 一款基于 jQuery 的弹窗组件
    弹出图片层
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338254.html
Copyright © 2020-2023  润新知