注册 登录
编程论坛 C++教室

[求助]实现数组A[n]中的所有元素循环左移k个位置(但我设计的算法却不能用)

discus815 发布于 2007-10-29 22:34, 1157 次点击

本人是菜鸟,高手帮忙调试一下啊!
#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;
}

9 回复
#2
雨中飞燕2007-10-29 23:54
开两个数组直接复制就是了,干嘛搞这么麻烦。。。。。。。。



by 雨中飞燕 C/C++学习讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge)论坛:[/url] http://yzfy.programfan.com

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=181314" target="_blank">https://yzfy.org/
Blog: http://yzfy.programfan.com

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=181314
]C++编写的Windows界面游戏[/url]
#3
p03032302007-10-30 08:56

error C2133: 'r' : unknown size
d:\vc\c\2.cpp(79) : error C2440: '=' : cannot convert from 'void' to 'int []'
There are no conversions to array types, although there are conversions to references or pointers to arrays
执行 cl.exe 时出错.


#4
p03032302007-10-30 08:57

#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[5];
Converse(A,5,1);

cout<<"after converse,the data are:";
for(int j=0;j<5;j++)
cout<<A[j];
cout<<endl;
}
设计思想挺好 学习了
要是我一看到这题目首先想的就是移动

#5
discus8152007-10-30 12:57

我自己也调试了一份(有网友的帮助),用指针实现:
#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;
}

#6
冰烨2007-10-31 15:44
回复:(雨中飞燕)开两个数组直接复制就是了,干嘛搞...
同意...
#7
jzwise2007-11-01 07:51

忽忽
~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]);
不太明白这个语句~

#8
csmenglei9512007-11-01 13:55

数组下标加上n-k后对n取模就行了吧

#9
六道2007-11-01 23:16
#include<stdio.h>
#include <math.h>
main()
{
int i,j,n,m,a[10],b[10],k,q,p,x; //n数组位数,m后移位数
printf("input two number:");
scanf("%d%d",&n,&m);
printf("input array is:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
x=m;

for(i=n-1;i>=0;i--)
{
q=0;m=x; //注意:由于下面m值在递减,这里要重新给M赋值
for(;m>0;m--)
{b[q]=a[n-m];q++;}
q--;

for(j=n-1-x;j>=0;j--)
a[j+x]=a[j];

for(p=q;q>=0;q--,p--)
a[p]=b[q];

for(k=0;k<n;k++)
printf("%3d",a[k]);
printf("\n");
}

}
#10
六道2007-11-03 00:11
回复:(雨中飞燕)开两个数组直接复制就是了,干嘛搞...

#include<iostream.h>

void main()
{
int move,size,p;
int a[20],b[20]={0};
cout<<"输入数组大小:";
cin>>size;
cout<<"每次移动几位:";
cin>>move;
cout<<"输入数组:";
for(int i=0;i<size;i++)
cin>>a[i];
cout<<"输出数组如下:"<<endl;
for(int n=0 ; n < size ; n++)
{
p=move;
int i=0,j=0;
while(p != 0)
{
b[j]=a[size-p];
j++; p--;
}
while(i < (size-p))
{
b[j] = a[i];
j++; i++;
}
for(i = 0;i < size; i++)
{
a[i] = b[i];
cout<<b[i]<<" ";
}
cout<<endl;
}
}
红色注意下,没此句的话,输出数组都一样的~

[此贴子已经被作者于2007-11-3 0:12:21编辑过]

1