数组的循环移位问题

wang_qiao_ying 2010-04-01 08:07:54


定义一个字符数组,实现循环右移动n位置.

char str[] = "ABCDEFG";

函数接口:char *shift(char *str, int n)

例如:当n=3时, 移动后的效果为"EFGABC",哪位哥哥能给个好一点的算法??

...全文
557 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_qiao_ying 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jackyid 的回复:]
C/C++ code

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <assert.h>

char *shift ( char *str, int n )
{

int length;
char *p;

assert ( str !=NULL );

lengt……
[/Quote]
这位的实现还不错,不过我想能不能有更好的办法发,不靠申请多余的内存来实现。
wang_qiao_ying 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tianweishuiguo 的回复:]
C/C++ code

char *shift(char *str, int n)
{
int i = 0,tmp = 0;
int len = strlen(str);
len = len-n;
printf("%d",len);
for(;i<n;i++,len++)
{
tmp = str[len];
str[len] = str……
[/Quote]
这个实现有点缺陷,如果移动超过了字符串的长度会出错,至少应该有个%取余的操作,是不是?
Thirty 2010-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tianweishuiguo 的回复:]

C/C++ code

char *shift(char *str, int n)
{
int i = 0,tmp = 0;
int len = strlen(str);
len = len-n;
printf("%d",len);
for(;i<n;i++,len++)
{
tmp = str[len];
str[len] = str[i];
……
[/Quote]程序写错了
电信用户 2010-04-03
  • 打赏
  • 举报
回复
牛人啊 我脑子反应不过来,我今天才学的指向函数的指针,嘿嘿
Thirty 2010-04-01
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <assert.h>

char *shift ( char *str, int n )
{

int length;
char *p;

assert ( str !=NULL );

length=strlen ( str );
p= ( char * ) malloc ( sizeof ( char ) * ( length+1 ) );
if ( n <=length )
{
strcpy ( p,str+length-n );
strncat ( p,str,length-n );
* ( p+length ) ='\0';
strcpy ( str,p );
}
else
{
strcpy ( p,str+length- ( n%length ) );
strncat ( p,str,length- ( n%length ) );
* ( p+length ) ='\0';
strcpy ( str,p );
}

free ( p );

return str;
}

int main()
{
char str[] = "ABCDEFG";
printf ( "%s\n",shift ( str,8 ) );

return 0;
}
Thirty 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jiao319 的回复:]
各位大牛,人家要的是算法。今年考研的计算机专业综合的算法设计就是类似的一个题。
[/Quote]
程序就是最直接的算法啊,那不对?
Jiao319 2010-04-01
  • 打赏
  • 举报
回复
各位大牛,人家要的是算法。今年考研的计算机专业综合的算法设计就是类似的一个题。
东大坡居士 2010-04-01
  • 打赏
  • 举报
回复

char *shift(char *str, int n)
{
int i = 0,tmp = 0;
int len = strlen(str);
len = len-n;
printf("%d",len);
for(;i<n;i++,len++)
{
tmp = str[len];
str[len] = str[i];
str[i] = tmp;
}
for(i = 0;i<strlen(str);i++)
{
printf("%c",str[i]);
}
}
DontKissBossAss 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liuxingjin 的回复:]
直接memmove(void *dest,void *scr,size_t count)就可以
[/Quote]


。。。。。无语了,这个真可以。。
BT六眼飞鱼 2010-04-01
  • 打赏
  • 举报
回复
char str[] = "ABCDEFG";
char *shift(char *str, int n)
{
int nResult;
int lenStr = strlen(str);
char *temp = new char[lenStr];
for(int i = 0; i < lenStr; i++ )
{
nResult = (i - n)%lenStr;
temp[nResult] = str[i];
}
strcpy(str,temp);
delete temp;//这里应该用delete[]吧。。
return str;
}
liuxingjin 2010-04-01
  • 打赏
  • 举报
回复
直接memmove(void *dest,void *scr,size_t count)就可以
xiuxianshen 2010-04-01
  • 打赏
  • 举报
回复
很简单

char str[] = "ABCDEFG";
char *shift(char *str, int n)
{
int nResult;
int lenStr = strlen(str);
char *temp = new char[lenStr];
for(int i = 0; i < lenStr; i++ )
{
nResult = (i - n)%lenStr;
temp[nResult] = str[i];
}
strcpy(str,temp);
delete temp;
return str;
}
zyyoung 2010-04-01
  • 打赏
  • 举报
回复
strcpy
memcpy的移位拷贝
DontKissBossAss 2010-04-01
  • 打赏
  • 举报
回复
我写的话,会这么写

if(!str) return NULL;

if(n<=0) return str;

for(;(n/=7)<7;);
char *ctemp =new char[8];
int i=0;
char *it = str;
char *front =str;;
while(1)
{
if(i == (7-n-1)) break;
i++;
last++;
}
while((*ctemp++ = *it++)!=0);
while((×ctemp++ = *front++) && front!=(str-n))

就是先把要str内后N个字符,放到新数组的前半部分,然后把str的剩余的7-N部分放到后边
O(N)时间内完成,

自己写吧,我感觉我写的太罗嗦,变量太多,。。代码写的少啊

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧