循环移位的程序求解~~
c语言吧
全部回复
仅看楼主
level 7
脸滚键盘C 楼主
如acbd,循环1位就成了dabc,程序要求达到任意位,看这个样子是要用递归了,能完成1位的,n位的有问题,望指点
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *xhyou(char *res,char * dest,unsigned int wei)
{
char *r=res;
char *d=dest;
if(wei>1)
{
d=xhyou(xhyou(r,d,wei-1),d,1);//这步有问题。。。
}
if(wei==1)
{
char c=0;
int i=0;
int length=strlen(r);
c=*(r+length-1);
printf("1 %c\n",c);
while(*(r+i+1)!='\0')
{
*(d+i+1)=*(r+i);
printf("4%c\n",*(d+i+1));
i++;
}
*(d+i+1)='\0';
*d=c;
printf("111%s\n",d);
}
return d;
}
int main(void)
{
char *c="abcd";
char *d=NULL;
int length=sizeof(c);
char *dest=(char *)malloc(length*sizeof(char));
d=xhyou(c,dest,3);
//dest=xhyou(r,dest);
//printf("%s\n",dest);
//dest=xhyou(r,dest);
printf("%s\n",dest);
return 0;
}在线等,谢谢
2012年08月31日 09点08分 1
level 12
这用毛线递归
2012年08月31日 09点08分 2
用什么?
2012年08月31日 12点08分
level 11
#include<stdio.h>
main(bit,temp)
{
char ch[]="abcd";
for(scanf("%d",&bit);bit>0;bit--)
temp=ch[3],memmove(ch+1,ch,3),ch[0]=temp;
puts(ch);
}
2012年08月31日 09点08分 3
level 13
很难么???建个临时变量用来保存被移出来那个字节或者是位。
2012年08月31日 09点08分 4
我那个实现1位移动了,但是2位以上就出问题了。。。
2012年08月31日 12点08分
level 10
#include "string.h"
int turn(char a[]);
main()
{
char *a;
int n;
gets(a);
printf("你要移位的次数:");
scanf("%d",&n);
while(n--)
yturn(a);
puts(a);
system("pause");
}
int yturn(char a[])
{
int len,i;
char temp;
len=strlen(a);
temp=a[0];
for(i=0;i<len-1;i++) a[i]=a[i+1];
a[len-1]=temp;
}
感觉你写的好复杂,是我理解的问题吗,这是我写的,好像可以满足你所说的功能
2012年08月31日 14点08分 5
1