2014年第五届蓝桥杯C/C++ A组国赛 —— 第三题:日期差
生活随笔
收集整理的這篇文章主要介紹了
2014年第五届蓝桥杯C/C++ A组国赛 —— 第三题:日期差
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
標(biāo)題:日期差
歷史上,不同的人類聚居地可能有不同的歷法,因而記錄下來的資料中日期的換算就很麻煩。幸好今天我們統(tǒng)一使用公元紀(jì)年法。當(dāng)然,這種歷法對(duì)求兩個(gè)日期差多少天也不是十分簡(jiǎn)便,但畢竟是可以忍受的。
下面的程序計(jì)算了兩個(gè)日期的差值,兩個(gè)日期都使用公元紀(jì)年法。
請(qǐng)分析程序邏輯,并推斷劃線部分缺失的代碼。
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; }注意:通過瀏覽器提交答案。只填寫缺少的內(nèi)容,不要填寫任何多余的內(nèi)容(例如:說明性文字或已有符號(hào))。
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^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.101 10 1.1 ^^^1111110010 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. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 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^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 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; }總結(jié)
以上是生活随笔為你收集整理的2014年第五届蓝桥杯C/C++ A组国赛 —— 第三题:日期差的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2014年第五届蓝桥杯C/C++ A组国
- 下一篇: 2014年第五届蓝桥杯C/C++ A组国