递归产生回文数
原文链接:https://blog.csdn.net/qq_39355828/article/details/116008026 (opens new window)
# 递归产生回文数
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kXpJUbME-1619063964580)(C:\Users\123\AppData\Roaming\Typora\typora-user-images\1618797823638.png)]](/csdn-assets/116008026/image-01.png)
回文数 包括小写字母的随机产生,大写字母的随机产生,首先显示一个菜单,然后选中需要产生的回文数市数字,小写英文,还是大写英文,进而输入产生的回文数的长度。
// Program RollDice.cpp
// This program demonstrates using random numbers
// to simulate rolling dice.
#include <iostream>
#include <cstdlib> // rand and srand
#include <ctime> // For the time function
using namespace std;
void Intfunc(int n);
void showMenu();
void Upperfunc(int n);
void Intfunc1(int n);
void Upperfunc1(int n);
void Lowerfunc1(int n);
void Lowerfunc(int n);
void delay_sec(int sec);
int main()
{ int length,choice;
int ADULT_CHOICE=1;
int QUIT_CHOICE=4;
int m;
do
{
// Display the menu and get the user's choice.
showMenu();
cin >> choice;
// Validate the menu selection.
while (choice < ADULT_CHOICE || choice > QUIT_CHOICE)
{ cout<<choice;
cout << "is not a valid choice!\n";
cout<< "Please enter a number from 1 to 4.\n";
cout<<"Your choice?";
cin >> choice;
}
// If the user does not want to quit, proceed.
if (choice != QUIT_CHOICE)
{ cout<<"How long would you like your palindrome to be?";
cin>>length;
while (length < 0)
{
cout<< "Please enter a length grater than 0!\n";
cout<<"Your length?";
cin >> length;
}
cout<<"\nGenerating your palindrome...\n";
if(choice==1)
{
if(length%2==1)
{
m=length/2+1;
Intfunc1(m);
}
if(length%2==0)
{
m=length/2;
Intfunc(m);
}
}
if(choice==2)
{
if(length%2==1)
{
m=length/2+1;
Upperfunc(m);
}
if(length%2==0)
{
m=length/2;
Upperfunc1(m);
}
}
if(choice==3)
{
if(length%2==1)
{
m=length/2+1;
Lowerfunc(m);
}
if(length%2==0)
{
m=length/2;
Lowerfunc1(m);
}
}
}
} while (choice != QUIT_CHOICE);
cout<<"Thanks for using my palindrome generator!!"<<endl;
cout<<"Come back soon!"<<endl;
return 0;
}
void showMenu()
{ cout<<"\n------------------------------------"<<endl;
cout << "Welcome to the palindrome generate!\n\n"
<<"------------------------------------\n"
<<"Choose the type of palindrome you'd like to generate\n"
<<"from the menu below,or choose 4 to qiut the program.\n"
<< "1) Digits\n"
<< "2) Uppercase letters\n"
<< "3) Lowercase letters\n"
<<"4) Quite program\n"
<<"\n------------------------------------\n"
<< "Your choice? ";
}
void Intfunc(int n)//even number
{
unsigned int seed = (unsigned int)(time(0));
// Seed the random number generator.
srand(seed);
// Get some die values.
int value = rand() % 9 + 1;
//Sleep(1000);
delay_sec(5);
if(n==1)
{
printf("%d",value);
printf("%d",value);
}
else
{
printf("%d",value);
Intfunc(n-1);
printf("%d",value);
}
}
void Intfunc1(int n)//odd number
{
unsigned int seed = (unsigned int)(time(0));
// Seed the random number generator.
srand(seed);
// Get some die values.
int value = rand() % 9 + 1;
delay_sec(5);
if(n==1)
printf("%d",value);
else
{
printf("%d",value);
Intfunc1(n-1);
printf("%d",value);
}
}
void Upperfunc(int n)//A B C odd number
{
unsigned int seed = (unsigned int)(time(0));
int MAX_VALUE=90;
int MIN_VALUE=65;
// Seed the random number generator.
srand(seed);
int value= (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// Get some die values.
//int value = rand() % 9 + 1;
delay_sec(5);
if(n==1)
printf("%c",value);
else
{
printf("%c",value);
Upperfunc(n-1);
printf("%c",value);
}
}
void Upperfunc1(int n)//A B C even number
{
unsigned int seed = (unsigned int)(time(0));
int MAX_VALUE=90;
int MIN_VALUE=65;
// Seed the random number generator.
srand(seed);
int value= (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// Get some die values.
//int value = rand() % 9 + 1;
delay_sec(5);
if(n==1)
{
printf("%c",value);
printf("%c",value);
}
else
{
printf("%c",value);
Upperfunc1(n-1);
printf("%c",value);
}
}
void Lowerfunc(int n)//odd number
{
unsigned int seed = (unsigned int)(time(0));
int MAX_VALUE=122;
int MIN_VALUE=97;
// Seed the random number generator.
srand(seed);
int value= (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// Get some die values.
//int value = rand() % 9 + 1;
delay_sec(5);
if(n==1)
printf("%c",value);
else
{
printf("%c",value);
Lowerfunc(n-1);
printf("%c",value);
}
}
void Lowerfunc1(int n)//even number
{
unsigned int seed = (unsigned int)(time(0));
int MAX_VALUE=122;
int MIN_VALUE=97;
// Seed the random number generator.
srand(seed);
int value= (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// Get some die values.
//int value = rand() % 9 + 1;
delay_sec(5);
if(n==1)
{
printf("%c",value);
printf("%c",value);
}
else
{
printf("%c",value);
Lowerfunc1(n-1);
printf("%c",value);
}
}
void delay_msec(int msec)
{
clock_t now = clock();
while(clock()-now < msec);
}
void delay_sec(int sec)//
{
time_t start_time, cur_time;
time(&start_time);
do
{
time(&cur_time);
} while((cur_time - start_time) < sec);
}
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
需要注意:随机数的产生和系统的时间有关系,随机数时间如果相邻太短,则相对的时间太少,产生的随机数可能会一致,因此考虑使用延时函数
编辑 (opens new window)
上次更新: 2026/08/11, 13:36:18