問題:
あなたの一生を24時間にたとえると今日は何時何分何秒ですか?
ただしあなたはあなたの誕生日(a年b月c日)の0時ちょうどに生まれてn歳まで生きる(n歳のときは生きていてn+1歳にはなれない)とし、bとcは一般的な月日の範囲とします。
i) 1990<=a<=2000,n=80のとき、今日は何時何分何秒ですか?
ii) 1900<=a<=2000,n=200のとき、今日は何時何分何秒ですか?
回答(C++プログラムコード):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
#include<cstdio> #include<cstdlib> #include<iostream> #include<ctime> using namespace std; // check if the year is a leap year bool is_leap_year(int year) { if (year%4==0 && year%100!=0 || year%400==0) return true; else return false; } // check if the day is valid bool is_birthday_valid(int year, int month, int day) { switch(month) { case 1: if (day>0 && day<32) return true; else return false; break; case 2: if (is_leap_year(year)) { if (day>0 && day<30) return true; else return false; } else { if (day>0 && day<29) return true; else return false; } break; case 3: if (day>0 && day<32) return true; else return false; break; case 4: if (day>0 && day<31) return true; else return false; break; case 5: if (day>0 && day<32) return true; else return false; break; case 6: if (day>0 && day<31) return true; else return false; break; case 7: if (day>0 && day<32) return true; else return false; break; case 8: if (day>0 && day<32) return true; else return false; break; case 9: if (day>0 && day<31) return true; else return false; break; case 10: if (day>0 && day<32) return true; else return false; break; case 11: if (day>0 && day<31) return true; else return false; break; case 12: if (day>0 && day<32) return true; else return false; break; } return true; } // calculate days after the given day, not including the given day (return 0 when given 12/31) int calculate_days_after(bool leap, int month, int day) { int days=0; int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31}; if (leap) { days_of_month[1]=29; } if (month<12) for (int i=month;i<12;i++) { days+=days_of_month[i]; } days+=(days_of_month[month-1]-day); return days; } // calculate days before the given day, including the given day (return 366 when given a leap year's 12/31) int calculate_days_before(bool leap, int month, int day) { int days=0; int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31}; if (leap) { days_of_month[1]=29; } for (int i=0;i<month-1;i++) { days+=days_of_month[i]; } days+=day; return days; } // calculate the number of years that are leap years between the startyear and endyear (including the startyear and endyear) int calculate_leap_year(int startyear, int endyear) { if (startyear<1904) { if (endyear>1903) return (endyear-1904)/4+1; else return 0; } else if (is_leap_year(startyear)) { return (endyear-startyear)/4+1; } else if (is_leap_year(startyear+1)) { return (endyear-startyear-1)/4+1; } else if (is_leap_year(startyear+2)) { return (endyear-startyear-2)/4+1; } else if (is_leap_year(startyear+3)) { return (endyear-startyear-3)/4+1; } return 0; } // calculate the total days in n years int calculate_total_days(int startyear, int n, int month, int day) { return (n-1)*365+calculate_leap_year(startyear+1, startyear+n-1)+ calculate_days_after(is_leap_year(startyear),month,day)+calculate_days_before(is_leap_year(startyear+n),month,day); } int calculate_current_days(int startyear, int startmonth, int startday, int year, int month, int day) { return (year-startyear-1)*365+calculate_leap_year(startyear+1, year-1)+ calculate_days_after(is_leap_year(startyear),startmonth,startday)+calculate_days_before(is_leap_year(year),month,day); } int main() { // declaration struct tm *date; time_t now; int year, month, day; // current time int birthyear,birthmonth,birthday; // birthday float total_days; // total days of n years float current_days; // total days since birthday int current_seconds; // time in 24 hours int current_minutes; int current_hours; int n; // life span // get current time time(&now); date = localtime(&now); year = date->tm_year + 1900; month = date->tm_mon + 1; day = date->tm_mday; // input data int problemnum; cout<<"Please input problem number(1-2):"<<endl; do cin>>problemnum; // select problem number while(!(problemnum==1 || problemnum==2)); if (problemnum==1) { n=80; // set life span cout<<"Please input the year you birth(1990-2000):"<<endl; do cin>>birthyear; // get birthyear while(birthyear<1990 || birthyear>2000); } else { n=200; // set life span cout<<"Please input the year you birth(1900-2000):"<<endl; do cin>>birthyear; // get birthyear while(birthyear<1900 || birthyear>2000); } cout<<"Please input the month you birth:"<<endl; do cin>>birthmonth; // get birthmonth while(birthmonth<1 || birthmonth>12); cout<<"Please input the day you birth:"<<endl; do cin>>birthday; // get birthday while(!is_birthday_valid(birthyear,birthmonth,birthday)); // check if the birthday is valid // calculate the result current_days=calculate_current_days(birthyear,birthmonth,birthday,year,month,day); // calculate total days since birthday total_days=calculate_total_days(birthyear,n,birthmonth,birthday); // calculate total days of n years since birthday current_seconds=(int)(current_days/total_days*3600*24); // calculate the time in 24 hours current_minutes=current_seconds/60; current_hours=current_minutes/60; current_seconds=current_seconds%60; current_minutes=current_minutes%60; // output the result cout<<"あなたの一生を24時間にたとえると今日は "<<current_hours<<":"<<current_minutes<<":"<<current_seconds<<endl<<endl; system("PAUSE"); return 0; } |
プログラムコード:LifeClock.cpp
URL: http://leybreeze.com/blog/wp-content/uploads/2012/02/LifeClock.cpp
回答できた問題の出力:
入力によって違います。
アルゴリズムの簡単な説明:
生年月日と現在の日付を取得
誕生日からn歳までの日数と誕生日から今までの日数を計算
24時間に変換
秒:誕生日から今までの日数/誕生日からn歳までの日数*3600*24
秒をよって分と時を計算
実行方法:LifeClock.exe 実行して小問の番号と生年月日を入力してください
URL: http://leybreeze.com/blog/wp-content/uploads/2012/02/LifeClock.exe
実行環境:Windows XP/Vista/7
ライセンス:GPLv3