Skip to content

Commit 2969746

Browse files
author
wangxx
committed
递归和迭代法来演示数学归纳法
1 parent 0772c3e commit 2969746

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

AK-wang/A0001/prove.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<stdio.h>
2+
//递归和归纳都是将复杂问题简单化的方法,只是方向不同,递归是"从一般到个别",归纳是"从个别到一般"
3+
4+
//用递归的思想来证明数学归纳法
5+
void prove1(int n)
6+
{
7+
if(n == 0){
8+
printf("step 0,the P(%d) true.\n", n);
9+
}else{
10+
prove1(n-1);
11+
printf("step %d,if P(%d) true ,then P(%d) true also.\n", n, n - 1, n);
12+
printf("so,we can say \"P(%d) true.\"\n", n);
13+
14+
}
15+
}
16+
17+
//用循环来表示数学归纳法
18+
void prove2(int n)
19+
{
20+
int k;
21+
22+
printf("now we'll prove P(%d) true.\n", n);
23+
k = 0;
24+
printf("step 0 can prove P(%d) true.\n", k);
25+
while(k < n)
26+
{
27+
printf("step %d can say \" if P(%d) true,then P(%d) true also\".\n", k + 1, k, k + 1);
28+
k = k + 1;
29+
}
30+
printf("All prove over.\n");
31+
}
32+
33+
int main()
34+
{
35+
int n;
36+
printf("input your number:n=");
37+
scanf("%d",&n);
38+
printf("\n");
39+
prove1(n);
40+
printf("\n");
41+
prove2(n);
42+
return 0;
43+
}

0 commit comments

Comments
 (0)