﻿ 算法系列之归纳法

### 算法系列之归纳法

```#include <iostream>
using namespace std;
#define LENGTH 10
int data[LENGTH];

void SelectSort(int i,int num)
{
if(i < num)
{
int k = i;
for(int j = i+1;j <= num; j++)
if(data[j]<data[k])
k = j;
if(k!=i)
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
SelectSort(i+1,num);
}
}

int main()
{
int num;
cin>>num;
for(int i = 1;i <= num; i++)
cin>>data[i];
SelectSort(1,num);
for(int i = 1;i <= num; i++)
cout<<data[i]<<" ";
cout<<endl;
return 0;
}
```

插入排序：

```#include <iostream>
using namespace std;
#define LENGTH 10
int data[LENGTH];

void InsertSort(int i)
{
if(i > 1)
{
int x = data[i];
InsertSort(i-1);
int j = i - 1;
while(j>0&&data[j]>x)
{
data[j+1] = data[j];
j = j-1;
}
data[j+1] = x;
}
}

int main()
{
int num;
cin>>num;
for(int i = 1;i <= num; i++)
cin>>data[i];
InsertSort(num);
for(int i = 1;i <= num; i++)
cout<<data[i]<<" ";
cout<<endl;
return 0;
}
```

整数幂

```#include <iostream>
using namespace std;

int power(int x,int m)
{
int y;
if(m==0)
y = 1;
else
{
y = power(x,m/2);
y = y*y;
if(m%2==1)
y = x*y;
}
return y;
}

int main()
{
int num1,num2;
cin>>num1>>num2;
cout<<power(num1,num2)<<endl;
return 0;
}
```