1031. Hello World for U (20)

考察输出模拟,找输出形式的规律

#include<iostream>
#include<string.h>

int main()
{
	char str[100];
	while(scanf("%s", str) != EOF)
	{
		int n = strlen(str);
		int n1, n2;
		//find max n1, eg. min n2
		for(n2 = 3; n2 <= n; ++n2)
		{
			int temp = n+2-n2;
			if(temp%2 != 0)
				continue;
			n1 = temp/2;
			if(n1 <= n2)
				break;
		}
		//printf("%d %d %d\n",n,n2,n1);
		//output 'U' shape result
		for(int i = 0; i < n1; ++i)
		{
			
			printf("%c",str[i]);
			if(i != n1-1)
			{
				for(int j = 0; j < n2-2; ++j)
					printf(" ");
			}
			else
			{
				int k = i+1;
				for(int j = 0; j < n2-2; ++j,++k)
					printf("%c",str[k]);
			}
			printf("%c\n",str[n-1-i]);
		}

	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)