字符串旋转问题:"abcdefgh" 向左旋转3个字符,"defghabc"

 

 

int gcd(int a,int b)
{//求最大公约数
    if(a==0||b==0)
        return -1;
    int t=a;
    if(a<b)
    {        
        a=b;
        b=t;
    }
    while(b)
    {
      t=a%b;
      a=b;
      b=t;
    }
    return a;
}

void rotation(char *p,int n,int rotdist)
{//旋转
    int right=gcd(rotdist,n);
    for(int i=0;i<right;i++){
        char t=p[i];
        int j=i;
        while(true){
            int k=j+rotdist;
            if(k>=n)
                k-=n;
            if(k==i)
                break;
            p[j]=p[k];
            j=k;
        }
        p[j]=t;
    }
}

 

相关文章:

  • 2022-01-31
  • 2021-10-09
  • 2021-09-10
  • 2021-09-23
  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
猜你喜欢
  • 2021-11-03
  • 2021-09-14
  • 2021-10-22
  • 2022-12-23
  • 2021-09-08
  • 2021-05-29
相关资源
相似解决方案