, ( ) , , , , , .
. , .
() :
unsigned int getSum (unsigned int val) {
static const unsigned char lookup[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
:
18, 19, 20, 21, 22, 23, 24, 25, 26, 27
};
return (val == 0) ? 0 : getSum (val / 1000) + lookup[val%1000];
}
. 64- .
, ( , ), :
unsigned int getSum (unsigned int val) {
static const unsigned char lookup[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
:
18, 19, 20, 21, 22, 23, 24, 25, 26, 27
};
unsigned int tot = 0;
while (val != 0) {
tot += lookup[val%1000];
val /= 1000;
}
return tot;
}
, , , , . 10K 100K, , : -)
, , !
I prefer a more elegant recursive solution myself, but I am also one of those types who prefer mysterious crosswords. Read what you will.