A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
--------------------
- #include <stdio.h>
-
#include <string.h>
-
-
void itoa(char *buf, int n)
-
{
-
int i = 0;
-
-
for (i=0; n>0; i++, n /= 10) {
-
buf[i] = (n % 10) + '0';
-
}
-
-
buf[++i] = '\0';
-
}
-
-
int is_palindromic(int num)
-
{
-
char buf[sizeof(num)*8];
-
int len;
-
int i;
-
-
itoa(buf, num);
-
len = strlen(buf);
-
for (i=0; i<(len+1)/2; i++) {
-
if(buf[i] != buf[len-i-1])
-
return 0;
-
}
-
-
return 1;
-
}
-
-
int main(int argc, const char *argv[])
-
{
-
int i, j;
-
int result;
-
int acc, mi, mj;
-
-
for (i = 999; i > 100; i--) {
-
for (j = i; j > 100; j--) {
-
acc = i * j;
-
if (is_palindromic(acc) ) {
-
if (acc > result) {
-
result = acc;
-
mi = i, mj = j;
-
-
printf("result: %d, i %d, j %d\n", result, i, j);
-
}
-
}
-
}
-
}
-
-
printf(" %d = %d x %d\n", result, mi, mj);
-
-
return 0;
-
}
-
-
根据乘法交换律筛选了一些。再依据或然率可进一步优化。
阅读(1213) | 评论(0) | 转发(0) |