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
| int isRev_1(int i) { int rev = 0; int tmp = i;
while (tmp) { rev *= 10; rev += tmp % 10; tmp /= 10; }
return rev == i; }
int isRev_2(int i) { #define REV_TBL_SIZE 5 static int tbl[][REV_TBL_SIZE] = { {1000000001, 100000010, 10000100, 1001000, 110000}, { 100000001, 10000010, 1000100, 101000, 10000}, { 10000001, 1000010, 100100, 11000, 0}, { 1000001, 100010, 10100, 1000, 0}, { 100001, 10010, 1100, 0, 0}, { 10001, 1010, 100, 0, 0}, { 1001, 110, 0, 0, 0}, { 101, 10, 0, 0, 0}, { 11, 0, 0, 0, 0}, { 1, 0, 0, 0, 0}, { 0, 0, 0, 0, 0}, };
if (i < 0) { i = -i; }
int *tbl_p = (int *) tbl; while (i < *tbl_p) { tbl_p += REV_TBL_SIZE; }
int tbi, mask; for ( tbi = 0; tbi < REV_TBL_SIZE && (mask = tbl_p[tbi]); ++tbi ) { if (mask != 1000000001 && i >= 5 * mask) { i -= 5 * mask; } if (i >= 2 * mask) { i -= 2 * mask; } if (i >= mask) { i -= mask; } if (i >= mask) { i -= mask; } if (i & ((1 << tbi) - 1)) { return 0; } }
return !i; }
|