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 64 65 | public class Problem004 { public static void main(String[] args) { int num; boolean s1 = false, s2 = false, s3 = false; boolean f1 = false, f2 = false; int max = 0; for(int i = 100; i <= 999; ++i) { for(int j = 100; j <= 999; ++j) { num = i * j; // 10000 ~ 998001 //num이 여섯자리일 때 if(num >= 100000) { //일의 자리와 십만 자리 비교 if( (num / 100000) == (num % 10) ) { s1 = true; } else { s1 = false; } //십의 자리와 만의 자리 비교 if( ((num % 100) / 10) == ((num % 100000) / 10000) ) { s2 = true; } else { s2 = false; } //백의 자리와 천의 자리 비교 if( ((num % 1000) / 100) == ((num % 10000) / 1000) ) { s3 = true; } else { s3 = false; } if(s1 && s2 && s3) { if(num > max) { max = num; } } } else { // num < 100000 //일의 자리와 만의 자리 비교 if( (num % 10) == (num / 10000) ) { f1 = true; } else { f1 = false; } //십의 자리와 천의 자리 비교 if( ((num % 100) / 10) == ((num % 10000) / 1000) ) { f2 = true; } else { f2 = false; } if(f1 && f2) { if(num > max) { max = num; } } } } } System.out.printf("The largest pailndromic number: %d\n", max); } } | cs |
1년 전에 짠 코드.
코드가 너무 길어서 다시 했다.
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 | #include <stdio.h> int run(); int main() { return run(); } int run() { int n; int max = -1; int i, j; for (i = 100; i <= 999; ++i) { for (j = 100; j <= 999; ++j) { n = i * j; if (n < 100000) { if (n / 10000 == n % 10) { if ((n % 10000) / 10000 == (n % 100) / 10) { if (max < n) { max = n; } } } } else { if (n / 100000 == n % 10) { if ((n % 100000) / 10000 == (n % 100) / 10) { if ((n % 10000) / 1000 == (n % 1000) / 100) { if (max < n) { max = n; } } } } } } } printf("max: %d\n", max); return 0; } | cs |
좀 다듬었을 뿐 기본 알고리즘은 같다.
다른 방법은 도저히 모르겠다.
* 2021/01/17
아니 왜 문자열로 할 생각을 못 한 것이지??
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 | public class MyClass { public static void main(String args[]) { System.out.println(run()); } public static String run() { int max = -1; for(int a = 111; a < 1000; a++) { for(int b = 111; b < 1000; b++) { int n = a*b; if((n > max) && isSymmetric(n)) { max = n; } } } return Integer.toString(max); } public static boolean isSymmetric(int n) { String s = Integer.toString(n); int idx = s.length() / 2; while(idx > 0) { if(s.charAt(idx - 1) != s.charAt(s.length() - idx--)) return false; } return true; } } | cs |
9~10행:
폐구간 [111, 999]에 속하는 자연수, 즉 세 자리 자연수 a 와 b 에 대해 n 에 a*b 를 대입한 후 n 이 현재까지 수 중의 최대값인 max 보다 크고 대칭이면 max 에 n 을 대입한다.
22행~30행:
주어진 정수가 대칭수면 true , 아니면 false 를 return 한다.
https://t0pli.tistory.com/143 끝 부분 참고.
'Programming > Solutions' 카테고리의 다른 글
[Project Euler] Problem 005 (0) | 2019.03.20 |
---|---|
[BAEKJOON] 11721번 (0) | 2019.03.20 |
[Project Euler] Problem 003 (0) | 2019.03.19 |
[BAEKJOON] 2839번: 설탕 배달 (0) | 2019.03.19 |
[BAEKJOON] 1002번: 터렛 (0) | 2019.03.19 |