HDU_2137circumgyrate the string

本文详细解读了如何通过编程实现给定字符串逆时针旋转45度的操作,包括输入输出规范、核心算法思路及具体代码实现。通过8次循环模拟旋转过程,确保了字符串在不同旋转次数下的正确输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


代码:

    #include<iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    char a[100];
    int n;

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



思路解析:

题意:

输入一字符串“a”,数字“n”.

输出:把字符串“a”,以n(为正数时)次逆时针旋转每次45度。(n为负数时表示顺时针旋转)

360度,每45度为一次,共8次,故8为一整个一次大循环。模拟这8个即可。(n为负数时,n与 n+8相同)

见下图:

abcef 0
abcef
abcdef 1
     f
    e
   d
  c
 b
abcdef 2
   f
   e
   d
   c
   b
   a
abcdef 3
f
 e
  d
   c
    b
     a
abcdef 4
fedcba
abcdef 5
     a
    b
   c
  d
 e
f
abcdef 6
   a
   b
   c
   d
   e
   f
abcdef 7
a
 b
  c
   d
    e
     f
abcdef 8
abcdef
abcdef -1
a
 b
  c
   d
    e
     f
abcdef -2
   a
   b
   c
   d
   e
   f

。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值