広島大学 先進理工系科学研究科 情報科学プログラム 2018年1月実施 専門科目II 問題3
Author
祭音Myyura
Description
図1および図2に示すC言語による関数 power1, power2 は引数
(1) 図1で示す関数 power1 において、引数
(2) 図2に示す関数 power2 は再帰的に
(3) ある環境で関数 power1 を引数
(4) 図2に示す関数 power2 を参考にし、正の整数
Figures 1 and 2 show the functions power1 and power2 written in C.
These functions calculate
(1) When power1 in Figure 1 is executed with
(2) The function power2 in Figure 2 calculates
(3) When power1 in Figure 1 is executed with
(4) Referring to the function power2 in Figure 2, define the function factorial which calculates the factorial of the positive integer value
Figure 1
int power1(int x, int y) {
int i, num = 1;
if (y > 0) {
for (i = 1; i <= y; i++) {
num = num * x;
printf("%d:%d\n", i, num);
}
return num;
} else exit(1);
}
Figure 2
int power2(int x, int y) {
if (y > 0) {
if ( [a] ) return [b];
else return [c];
} else exit(1);
}
Figure 3
9: 1000000000
10: 1410065408
Kai
(1)
1:2
2:4
3:8
4:16
(2)
- [a]: y > 1
- [b]: x * power2(x, y - 1)
- [c]: x
(3)
(4)
int factorial(int n) {
if (n >= 1)
return n * factorial(n-1);
else
return 1;
}