HDU 2137 circumgyrate the string

本文介绍了一个字符串旋转的问题,通过输入一个字符串和一个整数N,将该字符串按逆时针方向旋转N次,每次45度,并展示旋转后的效果。文章提供了一段C++代码实现,该算法能够处理长度不超过80且始终为奇数的字符串。

circumgyrate the string

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4534    Accepted Submission(s): 1053


Problem Description
  Give you a string, just circumgyrate. The number N means you just   circumgyrate the string N times, and each time you circumgyrate the string for 45 degree anticlockwise.
 

Input
  In each case there is string and a integer N. And the length of the string is always odd, so the center of the string will not be changed, and the string is always horizontal at the beginning. The length of the string will not exceed 80, so we can see the complete result on the screen.
 

Output
  For each case, print the circumgrated string.
 

Sample Input
  
asdfass 7
 

Sample Output
  
a s d f a s s
 

Author
wangye
 

Source



模拟控制格式输出即可


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main()
{
    char s[85]={};
    int n,k;
    while(scanf("%s%d",s,&n)!=EOF)
    {
    int len=strlen(s);
    k=n%8;
    if(k<0)
        k+=8;
    if(k==2)
    {
        for(int i=len-1;i>=0;i--)
        {
            printf("%*c\n",(len+1)/2,s[i]);
        }
    }
    else if(k==4)
    {
        for(int i=len-1;i>=0;i--)
        {
            printf("%c",s[i]);
        }
        printf("\n");
    }
    else if(k==6)
    {
        for(int i=0;i<len;i++)
        {
            printf("%*c\n",(len+1)/2,s[i]);
        }
    }
    else if(k==0)
    {
        printf("%s\n",s);
    }
    else if(k==1)
    {
        for(int i=len-1,j=len;i>=0;i--,j--)
        {
            printf("%*c\n",j,s[i]);
        }
    }
    else if(k==3)
    {
        for(int i=len-1,j=1;i>=0;i--,j++)
        {
            printf("%*c\n",j,s[i]);
        }
    }
    else if(k==5)
    {
        for(int i=0;i<len;i++)
        {
            printf("%*c\n",len-i,s[i]);
        }
    }
    else if(k==7)
    {
        for(int i=0;i<len;i++)
        {
            printf("%*c\n",i+1,s[i]);
        }
    }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值