标题:[求助]实现数组A[n]中的所有元素循环左移k个位置(但我设计的算法却不能用) ...
取消只看楼主
discus815
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2007-10-11
 问题点数:0 回复次数:1 
[求助]实现数组A[n]中的所有元素循环左移k个位置(但我设计的算法却不能用)

本人是菜鸟,高手帮忙调试一下啊!
#include<iostream.h>


void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
}

void Reverse(int A[],int from,int to)
{
for(int i=0;i<(to-from+1)/2;i++)
swap(A[from+i],A[to-i]);
}

void Converse(int A[],int n,int k)
{
Reverse(A,0,k-1);
Reverse(A,k,n-1);
Reverse(A,0,n-1);
}

void main()
{
int A[]={1,2,3,4,5};
cout<<"before converse ,the data are:";
for(int i=0;i<5;i++)
{
cout<<A[i];
}
cout<<endl;
int r[];
r=Converse(A,5,1);
cout<<"after converse,the data are:";
for(int j=0;j<5;j++)
cout<<r[j];
cout<<endl;
}

搜索更多相关主题的帖子: int Converse 算法 Reverse 元素 
2007-10-29 22:34
discus815
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2007-10-11
得分:0 

我自己也调试了一份(有网友的帮助),用指针实现:
#include<iostream.h>


void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
}

void Reverse(int A[],int from,int to)
{
for(int i=0;i<(to-from+1)/2;i++)
swap(A[from+i],A[to-i]);
}

int *Converse(int A[],int n,int k)
{
Reverse(A,0,k-1);
Reverse(A,k,n-1);
Reverse(A,0,n-1);
return A;
}

void main()
{
int A[]={1,2,3,4,5};
int *r;
cout<<"before converse ,the data are:";
for(int i=0;i<5;i++)
{
cout<<A[i];
}
cout<<endl;
r=Converse(A,5,1);
cout<<"after converse,the data are:";
for(int j=0;j<5;j++)
cout<<r[j];
cout<<endl;
}


旋转的木马,没有翅膀,但是却可以带着你飞翔.......
2007-10-30 12:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-181822-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.976991 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved