1

So, I've been trying to solve TopCoder SRM 145, div2 500 points question: http://community.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm145

The way I've tried to solve is by checking all the multiples of the 1% of the total time given and counting only those which are integers. This is the code I wrote in the usual format to run it on an online compiler.

#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;
}

Output:

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

My question is that why does the program show only two "equals" in the output list, when 168, 252, 336 are equal too? Also, why do they stop being equal in these regular intervals, like, 672 and 671? Thanks.

  • When you print I suspect it is not showing the full precision and chopping off some decimal places. – Neil Kirk Apr 04 '15 at 00:04
  • Try printing more precision: cout << fixed << setprecision(5) << S1; – Buddy Apr 04 '15 at 00:06
  • output the value of (S1 - a) and the answer may pop out at you. I'm pretty sure it's because double doesn't have many exact matches to integer values and instead it's something very close to the int value – Sten Petrov Apr 04 '15 at 00:07
  • The integer part of the `double` may not actually fit into an `int`. – jxh Apr 04 '15 at 00:12

0 Answers0