Hey there. Haven't been coding in a while, but I am doing challenges on DareYourMind.net and can't figure why I do not get the correct answer. I've picked quite a few numbers (~20-30) and calculated by hand the results and I get the same as the output from my program.
Basically, I have to input the total number of letters of all the written numbers from 1 to 2008.
Here's what I have coded, I commented it as much as possible for you guys:Any help is much welcome !Code:#include <iostream> int main() { int total = 0, current = 0, temp = 0; for(int i = 1; i < 2009; i++) { current = 0; // if Xxxx is non-zero, then must add "one/two thousand" if(i / 1000) current += 11; // if xXxx is non-zero, then must add "one...nine" // and if xxXX is non-zero, then must add "hundred and" // otherwise add "hundred" if(i / 100 && ((i / 100) % 10)) { if(i % 100 == 0) current += 7; // hundred else current += 10; // hundred and temp = (i / 100) % 10; switch(temp) { case 1: case 2: case 6: current += 3; break; case 3: case 7: case 8: current += 5; break; case 4: case 5: case 9: current += 4; break; } } // if xxXx is non-zero, then add "ten...ninety" // separate case for numbers between 11 and 19 for "eleven...nineteen" if((i / 10) && ((i / 10) % 10)) { temp = i % 100; if(temp > 10 && temp < 20) { switch(temp) { case 11: case 12: current += 6; break; case 16: case 15: current += 7; break; case 17: current += 9; break; case 13: case 18: case 14: case 19: current += 8; break; } } else { temp = (i / 10) % 10; switch(temp) { case 1: current += 3; break; case 2: case 3: case 4: case 8: case 9: current += 6; break; case 5: case 6: current += 5; break; case 7: current += 7; break; } } } // make sure we're not in the "eleven...nineteen" range // and xxxX is non-zero, then add "one...nine" if((((i % 100) < 10) || ((i % 100) > 20)) && !(i % 10)) { temp = i % 10; switch(temp) { case 1: case 2: case 6: current += 3; break; case 3: case 7: case 8: current += 5; break; case 4: case 5: case 9: current += 4; break; } } std::cout << i << " : " << current << "\t"; total += current; } std::cout << std::endl << total; }
Thanks (:



LinkBack URL
About LinkBacks



