这是一个求水仙花数的题目(21的位的水仙花数)我想问这是什么问题啊?
我想问这到底是什么问题啊?请大家看看!
这是my.h文件
1 void add(char num1[],char num2[],char score[]);
2 void remove(char num[]);
3 void getbuffer(char buffer[],char num[],int len);
4 void carry(char buffer2[],char numbuffer[],int &n);
5 void add0(char buffer2[]);
6 void addremain(char num[],int len,char score[],int n);
7 void getthis(int i,char score[],char num[][25]);
#include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #include"my.h"
5 void add(char num[],char num2[],char score[])
6 {
7 char num1[25];
8 strcpy(num1,num);
9 int len1,len2,n=0,k,j;
10 char buffer1[5],buffer2[5],temp[25];
11 len1=strlen(num1);
12 len2=strlen(num2);
13 score[0]='\0';
14 temp[0]='\0';
15 while(len1>0&&len2>0)
16 {
17
18
19 getbuffer(buffer1,num1,len1);
20 getbuffer(buffer2,num2,len2);
21 len1-=3;len2-=3;
22 k=atoi(buffer1);j=atoi(buffer2);
23 carry(temp,score,n);
24 itoa(k+j+n,temp,10);
25 n=0;
26 add0(temp);
27 }
28 carry(temp,score,n);
29 if(len1>0)
30 {
31 addremain(num1,len1,score,n);
32 n=0;
33 }
34 if(len2>0)
35 {
36 addremain(num2,len2,score,n);
37 n=0;
38 }
39 if(n!=0)
40 {
41 itoa(n,temp,10);
42 strcat(temp,score);
43 strcpy(score,temp);
44 }
45 remove(score);
46 }
47 void getbuffer(char buffer[],char num[],int len)
48 {
49
50 if(len>3)
51 {
52 strncpy(buffer,num+len-3,3);
53 buffer[3]='\0';
54 }
55 else
56 {
57 strncpy(buffer,num,len);
58 buffer[len]='\0';
59 }
60 }
61 void carry(char buffer2[],char numbuffer[],int &n)
62 {
63 if(strlen(buffer2))
64 {
65 if(strlen(buffer2)>3)
66 {
67 strcat(buffer2,numbuffer);
68 n=buffer2[0]-48;
69 strcpy(numbuffer,buffer2+1);
70 }
71 else
72 {
73 strcat(buffer2,numbuffer);
74 strcpy(numbuffer,buffer2);
75 }
76 }
77 }
78 void add0(char buffer2[])
79 {
80 if(strlen(buffer2)<2)
81 {
82 buffer2[2]='\0';
83 buffer2[1]=buffer2[0];
84 buffer2[0]='0';
85 }
86 if(strlen(buffer2)<3)
87 {
88 buffer2[3]='\0';
89 buffer2[2]=buffer2[1];
90 buffer2[1]=buffer2[0];
91 buffer2[0]='0';
92 }
93 }
94 void addremain(char num[],int len,char score[],int n)
95 {
96 char buffer[25];
97 if(len!=0)
98 {
99
100 int k=num[len-1]-48+n;
101 itoa(k%10,buffer,10);
102 strcat(buffer,score);
103 if(k>9)
104 {
105 len--;
106 addremain(num,len,buffer,k/10);
107 strcpy(score,buffer);
108 }
109 else
110 {
111 strncpy(score,num,len-1);
112 score[len-1]='\0';
113 strcat(score,buffer);
114 }
115 }
116 else
117 {
118 itoa(n,buffer,10);
119 strcat(buffer,score);
120 strcpy(score,buffer);
121 }
122
123 }
124 void remove(char num[])
125 {
126 if(num[0]=='0')
127 {
128 char buffer[25];
129 if(num[1]=='0')
130 {
131 strncpy(buffer,num+2,strlen(num)-2);
132 buffer[strlen(num)-2]='\0';
133 }
134 else
135 {
136 strncpy(buffer,num+1,strlen(num)-1);
137 buffer[strlen(num)-1]='\0';
138 }
139 strcpy(num,buffer);
140 }
141
142 }
143 void main()
144 {
145 char num[10][25];
146 char temp[25];
147 char score[25];
148 for(int i=0;i<10;i++)
149 {
150 num[i][0]=i+48;
151 num[i][1]='\0';
152 }
153
154 for(int i=2;i<10;i++)
155 {
156 strcpy(score,num[i]);
157 for(int k=0;k<20;k++)
158 {
159 for(int j=0;j<i-1;j++)
160 {
161
162 add(score,num[i],score);
163 }
164 strcpy(num[i],score);
165 }
166 }
167
168 for(int j=9;j>=0;j++)
169 {
170 strcpy(score,num[j]);
171 getthis(1,score,num);
172 }
173 }
174
175 void getthis(int i,char score[],char num[][25])
176 {
177 char temp[25];
178 if(i<21)
179 {
180 for(int i=9;i>=0;i--)
181 { strcpy(temp,score);
182 add(temp,num[i],temp);
183 if(strlen(temp)<22)
184 getthis(i+1,temp,num);
185 }
186 }else
187 {
188 if(strlen(score)==21)
189 {
190 puts(score);
191 }
192 }
193 }