您的位置:首页 > 其它

nefuoj-1041:字符串变形记

2016-02-12 16:22 323 查看

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: