1 |/*///-----------------------------------------------------------------
2 | * Project Euler Problem 004
3 | * File Name : pe-004.c
4 | * Author : Yen-Chin,Lee
5 | * Email : coldnew.tw@gmail.com
6 | * Create Date : 2009/03/22 14:55:59
7 | * Description : This version I use malloc (though I do not need to use it),
8 | * if you don't know how to use malloc() and free(),
9 | * just remove it and add " char buffer[100] " in this file
10 | *
11 | * Problem :
12 | *
13 | * A palindromic number reads the same both ways.
14 | * The largest palindrome made from the product of two 2-digit numbers
15 | * is 9009 = 91 × 99.
16 | *
17 | * Find the largest palindrome made from the product of two 3-digit numbers.
18 | /*///---------------------------- Copyright (C) ,2009 coldnew --------
19 |
20 | #include <stdio.h>
21 | #include <stdlib.h>
22 | #include <string.h>
23 |
24 |
25 | int isPlain(int testNum)
26 | {
27 | char *buffer = (char *)malloc(sizeof(char) * 100);
28 | if (NULL == buffer) {
29 | fprintf(stderr,"Not enough memory\n");
30 | exit(1);
31 | }
32 |
33 | sprintf(buffer,"%d",testNum);
34 |
35 | int a = 0;
36 | int b = strlen(buffer)-1;
37 |
38 | while (a < b && buffer[a] == buffer[b]) {
39 | a++;
40 | b--;
41 | }
42 | free(buffer);
43 |
44 | return (a >= b);
45 | }
46 |
47 | int main( int argc, char **argv)
48 | {
49 | int i,j,k;
50 | int maxNum=0;
51 | for (i = 1;i < 1000;i++) {
52 | for (j = 1;j < 1000; j++) {
53 | k = i*j;
54 | if (k > maxNum && isPlain(k)) {
55 | maxNum = k;
56 | }
57 | }
58 | }
59 | printf("max=%d ",maxNum);
60 |
61 | return 0;
62 | }
63 |
2009年3月22日 星期日
Project Euler 004.c
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言