Categories

hoj 1384 Palindrome Number

用字符串模拟产生回文数,超时

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
#include <stdio.h>
#include <stdlib.h>
 
void next_palin(char*str_of_num,int* lenth_p);/*产生下一个回文数*/
 
int main()
{
    int num,lenth=1;
    int i,count=1;
    char str_of_num[30]="1";
 
    while (scanf("%d",&num)!=EOF)
    {
        while (count<num)
        {
            next_palin(str_of_num,&lenth);
            count++;
        }
 
        for (i=0;i<lenth;i++)
            putchar(str_of_num[i]);
        putchar(’\n’);
    }
 
    return 0;
}
 
void next_palin(char*str_of_num,int* lenth_p)/*产生下一个回文数*/
{
    int i;
    int flag=1;
 
    for (i=0;i<*lenth_p;i++)
    {
        if (str_of_num[i]!=9)
        {
            flag=0;
        }
    }
 
    if (flag)
    {
        (*lenth_p)++;
        str_of_num[0]=str_of_num[*lenth_p-1]=1;
 
        for (i=1;i<*lenth_p-1;i++)
        {
            str_of_num[i]=0;
        }
    }
    else
    {
        i=*lenth_p/2;
        str_of_num[i]++;
        str_of_num[*lenth_p-1-i]=str_of_num[i];
        while ((str_of_num[i]>9) && (i<*lenth_p-1))
        {
            str_of_num[i]=str_of_num[*lenth_p-i-1]=0;
            str_of_num[i+1]++;
            str_of_num[*lenth_p-i-2]++;
            i++;
        }
    }
}

1 comment to hoj 1384 Palindrome Number

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

*