-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion. Classic recursion problems
More file actions
264 lines (189 loc) · 4.95 KB
/
Recursion. Classic recursion problems
File metadata and controls
264 lines (189 loc) · 4.95 KB
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
256
257
258
259
260
261
262
263
# Sum of Integers from 1 to N
| | |
| :--- | :--- |
| **Input** | Standard input |
| **Output** | Standard output |
### Problem Statement
Given a natural number $N$, find the sum of all integers from 1 to $N$.
### Input Format
The input consists of a single natural number $N$ ($N < 1000$).
### Output Format
Print a single value — the sum of all integers from 1 to $N$.
### Examples
| Input | Output |
| :--- | :--- |
| `5` | `15` |
| `10` | `55` |
#include <iostream>
using namespace std;
int sum1toN(int n) {
if (n == 1) return 1;
return n + sum1toN(n - 1);
}
int main() {
int N;
cin >> N;
cout << sum1toN(N) << endl;
return 0;
}
# Recursive Power
| | |
| :--- | :--- |
| **Input** | Standard input |
| **Output** | Standard output |
### Problem Statement
Implement a function for recursive exponentiation of a real number.
### Input Format
The first and only line of input contains a real number $x$ and an integer $p$ ($0 \le p \le 10, |x| \le 1000, x \neq 0$), representing the exponent.
### Output Format
Print $x^p$ with a precision of at least 6 decimal places. It is guaranteed that the absolute value of the result does not exceed $1,000,000$.
### Notes
If you are solving this problem using Python, you may store both input parameters as `float`.
### Examples
| Input | Output |
| :--- | :--- |
| `2.0 10` | `1024.0000000000` |
| `-1.0 3` | `-1.0000000000` |
#include <iostream>
#include <iomanip>
using namespace std;
double p(double a, int b) {
if (b == 0)
return 1.0;
return a * p(a, b - 1);
}
int main() {
double a;
int b;
cin >> a >> b;
double c = p(a, b);
cout << fixed << setprecision(6) << c << endl;
return 0;
}
# Recursive Exponentiation
***
### Problem Statement
Implement a recursive function to raise a real number to a power.
***
### Input Format
The first and only line of input contains a real number $x$ and an integer $p$ ($0 \le p \le 10, |x| \le 1000, x \neq 0$), which is the exponent.
***
### Output Format
Print the result of $x^p$ with a precision of at least 6 decimal places. It is guaranteed that the absolute value of the answer does not exceed $1,000,000$.
***
### Notes
If you are solving this problem using Python, you may store both input parameters as `float`.
***
### Examples
| Input | Output |
| :--- | :--- |
| `2.0 10` | `1024.0000000000` |
| `-1.0 3` | `-1.0000000000` |
#include <iostream>
#include <iomanip>
using namespace std;
double p(double a, int b) {
if (b == 0)
return 1.0;
return a * p(a, b - 1);
}
int main() {
double a;
int b;
cin >> a >> b;
double c = p(a, b);
cout << fixed << setprecision(6) << c << endl;
return 0;
}
# Recursive Factorial
| | |
| :--- | :--- |
| **Input** | Standard input |
| **Output** | Standard output |
### Problem Statement
Implement a recursive function to calculate the factorial of a natural number:
- For **C++**: `int factorial(int a)`
- For **Python**: `factorial(a)`
The factorial of a number is the product of all natural numbers less than or equal to it. For example, $5! = 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1$.
### Input Format
The program receives a single integer $n$ ($1 \le n \le 8$).
### Output Format
Print a single integer — the factorial of $n$.
### Notes
**Important:** Submit only the function for verification.
### Examples
| Input | Output |
| :--- | :--- |
| `3` | `6` |
| `4` | `24` |
#include <iostream>
using namespace std;
int factorial(int a) {
if (a == 1) return 1;
return a * factorial(a - 1);
}
int main(){
int n;
cin >> n;
cout << factorial(n);
return 0;
}
# Recursive Digit Count
| | |
| :--- | :--- |
| **Input** | Standard input |
| **Output** | Standard output |
### Problem Statement
Write a recursive function to count the number of digits in a natural number.
### Input Format
The program receives a single natural number $n$ ($1 \le n \le 2^{16} - 1$).
### Output Format
Print a single integer — the number of digits in $n$.
### Examples
| Input | Output |
| :--- | :--- |
| `412` | `3` |
| `7777` | `4` |
#include <iostream>
using namespace std;
int countDigits(int n) {
if (n < 10) {
return 1;
}
return 1 + countDigits(n / 10);
}
int main() {
int n;
cin >> n;
cout << countDigits(n) << endl;
return 0;
}
# Recursive Sum of Digits
| | |
| :--- | :--- |
| **Input** | Standard input |
| **Output** | Standard output |
### Problem Statement
Write a recursive function to calculate the sum of the digits of a natural number.
### Input Format
The program receives a single natural number $n$ ($n < 2^{16}$).
### Output Format
Print a single integer — the sum of the digits of $n$.
### Examples
| Input | Output |
| :--- | :--- |
| `412` | `7` |
| `777` | `21` |
#include <iostream>
using namespace std;
int sumDigits(int n) {
if (n < 10)
return n;
return (n % 10) + sumDigits(n / 10);
}
int main() {
int n;
cin >> n;
cout << sumDigits(n) << endl;
return 0;
}