티스토리 뷰
Problem 4
앞에서부터 읽을 때나 뒤에서부터 읽을 때나 모양이 같은 수를 대칭수(palindrome)라고 부릅니다.
두 자리 수를 곱해 만들 수 있는 대칭수 중 가장 큰 수는 9009 (= 91 × 99) 입니다.
세 자리 수를 곱해 만들 수 있는 가장 큰 대칭수는 얼마입니까?
#include <stdio.h>
void main() {
int result = 0;
int length = 0;
char str[10];
int cnt = 0;
int max = 0;
for (int i = 100; i <= 999; i++) {
for (int j = 100; j <= 999; j++) {
result = i * j;
sprintf_s(str, sizeof(str), "%d", result);
length = strlen(str);
for (int len = 0; len < length / 2; len++) {
if (str[len] == str[length - len -1] ) {
cnt++;
}
if (cnt == length / 2) {
if (max < result) {
max = result;
}
}
}
cnt = 0;
}
}
printf("max : %d \n", max);
}
Comments