nefuoj-1041:字符串变形记

description

<span style="font-family:SimSun;font-size:18px;">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.
</span>

input

<span style="font-family:SimSun;font-size:18px;">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.
</span>

output

<span style="font-family:SimSun;font-size:18px;">For each case, print the circumgrated string.</span>

sample_input

<span style="font-family:SimSun;font-size:18px;">asdfass 7</span>

sample_output

<span style="font-family:SimSun;font-size:18px;">a
 s
  d
   f
    a
     s
      s</span>

题解:模拟,纯粹的模拟,而且还得挨个模拟,敲着好累啊。。。


code:

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

int main()
{
    char s[105];
    int n;
    while(cin>>s)
    {
        cin>>n;
        n%=8;
        int len=strlen(s);
        if(n==0)
        {
            puts(s);
            continue;
        }
        if(n==7)
        {
            for(int i=0; i<len; i++)
            {
                for(int j=0; j<i; j++)
                putchar(' ');
                printf("%c\n",s[i]);
            }
            continue;
        }
        if(n==6)
        {
            for(int i=0; i<len; i++)
            {
                printf("%c\n",s[i]);
            }
            continue;
        }
        if(n==5)
        {
            for(int i=len-1; i>=0; i--)
            {
                for(int j=0; j<i; j++)
                putchar(' ');
                printf("%c\n",s[len-i-1]);
            }
            continue;
        }
        if(n==4)
        {
            for(int i=len-1; i>=0; i--)
            putchar(s[i]);
            putchar('\n');
            continue;
        }
        if(n==3)
        {
            for(int i=len-1; i>=0; i--)
            {
                for(int j=0; j<len-i-1; j++)
                putchar(' ');
                printf("%c\n",s[i]);
            }
            continue;
        }
        if(n==2)
        {
            for(int i=len-1; i>=0; i--)
            {
                printf("%c\n",s[i]);
            }
            continue;
        }
        if(n==1)
        {
            for(int i=len-1; i>=0; i--)
            {
                for(int j=0; j<i; j++)
                putchar(' ');
                printf("%c\n",s[i]);
            }
            continue;
        }
    }
    return 0;
}


下边的code是学校给的答案,说起来用switch语句看起来的确会舒服一点,但是字符数并没有少多少,,。,,

code2:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    char str[100];
    int n,i,j;
    while(scanf("%s%d",str,&n)!=-1)
    {
        if(n>=0) n%=8;
        else
        {
            n%=8;n+=8;n%=8;
        }
        int len=strlen(str);
        switch(n)
        {
            case 0:
            printf("%s\n",str);
            break;
            case 1:
            for(i=len-1;i>=0;i--)
              for(j=0;j<=i;j++)
                if(j==i)  printf("%c\n",str[i]);
                else printf(" ");
            break;
            case 2:
            for(i=len-1;i>=0;i--)
              for(j=0;j<=len/2;j++)
                if(j==len/2) printf("%c\n",str[i]);
                else printf(" ");
            break;
            case 3:
            for(i=len-1;i>=0;i--)
              for(j=0;j<=len-1-i;j++)
                if(j==len-1-i)  printf("%c\n",str[i]);
                else printf(" ");
            break;
            case 4:
            for(i=len-1;i>=0;i--)
              printf("%c",str[i]);
            printf("\n");
            break;
            case 5:
            for(i=0;i<len;i++)
              for(j=0;j<=len-1-i;j++)
                if(j==len-1-i)  printf("%c\n",str[i]);
                else printf(" ");
            break;
            case 6:
            for(i=0;i<len;i++)
              for(j=0;j<=len/2;j++)
                if(j==len/2)  printf("%c\n",str[i]);
                else printf(" ");
            break;
            case 7:
            for(i=0;i<len;i++)
              for(j=0;j<=i;j++)
                if(j==i)  printf("%c\n",str[i]);
                else printf(" ");
            break;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值