所以,我一直试图解决TopCoder SRM 145,div2 500分问题:http://community.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm145
我试图解决的方法是检查给定总时间的1%的所有倍数,并仅计算整数。这是我用通常格式编写的代码,用于在在线编译器上运行它。
#include <iostream>
#include <string>
using namespace std;
bool isInt(double S1) {
int a = (int)S1;
cout << S1 << " " << a << " "; //for debugging
if(S1 - a == 0) {
cout << "equal" << endl;
return true; }
else {
cout << endl;
return false; }
}
int main() {
unsigned int hours, minutes, seconds, S;
double S1;
string time = "00:28:00";
hours = (time[0]-'0')*10 + (time[1]-'0') ;
minutes = (time[3]-'0')*10 + (time[4]-'0');
seconds = (time[6]-'0')*10 + (time[7]-'0');
S = hours*3600 + minutes*60 + seconds;
S1 = (double)S/100;
int count = 0;
double S2 = S1;
while(S1 < S) {
if(isInt(S1)) {
count++;
}
S1 += S2;
}
cout << count;
return 0;
}
输出:
16.8 16
33.6 33
50.4 50
67.2 67
84 84 equal
100.8 100
117.6 117
134.4 134
151.2 151
168 168
184.8 184
201.6 201
218.4 218
235.2 235
252 252
268.8 268
285.6 285
302.4 302
319.2 319
336 336
352.8 352
369.6 369
386.4 386
403.2 403
420 420
436.8 436
453.6 453
470.4 470
487.2 487
504 504
520.8 520
537.6 537
554.4 554
571.2 571
588 588 equal
604.8 604
621.6 621
638.4 638
655.2 655
672 671
688.8 688
705.6 705
722.4 722
739.2 739
756 755
772.8 772
789.6 789
806.4 806
823.2 823
840 839
856.8 856
873.6 873
890.4 890
907.2 907
924 923
940.8 940
957.6 957
974.4 974
991.2 991
1008 1007
1024.8 1024
1041.6 1041
1058.4 1058
1075.2 1075
1092 1091
1108.8 1108
1125.6 1125
1142.4 1142
1159.2 1159
1176 1175
1192.8 1192
1209.6 1209
1226.4 1226
1243.2 1243
1260 1259
1276.8 1276
1293.6 1293
1310.4 1310
1327.2 1327
1344 1343
1360.8 1360
1377.6 1377
1394.4 1394
1411.2 1411
1428 1427
1444.8 1444
1461.6 1461
1478.4 1478
1495.2 1495
1512 1511
1528.8 1528
1545.6 1545
1562.4 1562
1579.2 1579
1596 1595
1612.8 1612
1629.6 1629
1646.4 1646
1663.2 1663
1680 1679
2
我的问题是,为什么该计划只显示两个&#34;等于&#34;在输出列表中,当168,252,336相等时?另外,为什么他们在这些固定的时间间隔内不再平等,如672和671? 感谢。