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", 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", n);
	return 0;
}

posted @ 2019-05-23 07:17  AlexKing007  阅读(121)  评论(0编辑  收藏  举报