其实题目不难,只是自己水平不行,做了一天,主要是运用sprintf函数和atoi函数处理,感觉自己对回文处理的能力弱爆了!
从今天我不再轻易的说某某题水题一道,除非你5分钟可以解决,否则都不算水题!
中文题目,不解释!
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1282
贴个代码:
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
66
67 |
#include <stdio.h> #include <stdlib.h> #include <string.h> int palindrome( char
st[]) { int
k= strlen (st); int
i,j; int
da=0; for (i=0;i<k/2;i++) { if (st[i]==st[k-i-1])da++; } /*if(k%2) { for(i=0,j=k-1;i<k/2,j>k/2;i++,j--) if(st[i]==st[j])da++; } else { for(i=0,j=k-1;i<k/2,j>=k/2;i++,j--) if(st[i]==st[j])da++; }*/ //printf("%d---",da); if (da>=k/2) return
0; else
return 1; } int
main() { long
n,m; int
i,j; char
st1[1000],st2[1000]; long
st3[1000]; while ( scanf ( "%ld" ,&n)!=EOF) { memset (st1,0, sizeof (st1)); memset (st2,0, sizeof (st2)); memset (st3,0, sizeof (st3)); int
data=0; sprintf (st1, "%ld" ,n); st3[0]=n; while (palindrome(st1)) { for (i= strlen (st1)-1,j=0;i>=0,j< strlen (st1);i--) { st2[j++]=st1[i]; } //printf("%d**",atoi(st2)); n+= atoi (st2); data++; st3[data]=n; sprintf (st1, "%ld" ,n); } printf ( "%d\n" ,data); for (i=0;i<data;i++) { printf ( "%ld--->" ,st3[i]); } printf ( "%ld\n" ,n); } return
0; } |
原文:http://www.cnblogs.com/ccccnzb/p/3596594.html