不动点问题的解决

来源:互联网 发布:淘宝鹊桥活动佣金插件 编辑:程序博客网 时间:2024/04/29 15:08
这是《算法设计与分析》一书习题2-10的实现代码:

#include 
<iostream.h>
#include 
<iomanip.h>
void reverse(int a[], int k, int n){
        
    
int temp;             /*O(1)的辅助空间*/
    
int j=n;
    
for(int i=k;i<=j;i++)
    
{
        temp
=a[i];
        a[i]
=a[j];
        a[j
--]=temp;
    }

}

void main(){
    
int a[]={1,2,3,4,5,6,7,8,9,10,11};
    
int k=3;
    
int n=11;

      
    cout
<<"********************************************************* ";
    cout
<<"* 习题2-10:数组逆序 * ";
    cout
<<"********************************************************* ";
    cout
<<"逆序前的数组:"<<endl;
    
for(int i=0;i<n;i++)
    
{
        cout
<<setw(3)<<a[i];
    }

    cout
<<endl;
    reverse(a,
0,k);
    reverse(a,k
+1, n-1);
    reverse(a,
0, n-1);

    cout
<<"从第"<<k+1<<"个位置后逆序后的数组:"<<endl;
    
for(int j=0;j<n;j++)
    
{
        cout
<<setw(3)<<a[j];
    }

    cout
<<endl;
}


 
原创粉丝点击