PAT 1031. Hello World for U (20)

1031. Hello World for U (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, "helloworld" can be printed as:

h  d
e  l
l  r
lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

Input Specification:

Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.

Output Specification:

For each test case, print the input string in the shape of U as specified in the description.

Sample Input:
helloworld!
Sample Output:
h   !
e   d
l   l
lowor

提交代码

/*
	本题不可另用二维数组保存最终结果然后输出,即使完全正确,AC仍是 0分。。。。。。。。。。。。
        1. 关键是要精确计算确定n1,n2,n3关于n的表达式。本题已给,难度骤降。
	2. 每次使用完要清空数组。。
	3. 思路清晰,不怕暴力扫描。
	4.strcpy(a,b)是把字符串赋值,而不是字符!!把b拷贝给a.可对字符直接赋值,对字符串不能直接赋值。char a=' ',不是双引号。。
*/
#include<stdio.h>
#include<string.h>
const int MAX=100;
int n1,n2,n3;

void cntN1(int n)  //执行完后已计算出n1,n2,n3
{
	int i,j;
	n1=-1;
	for(i=0;i<n;i++){
		for(j=0;j<=i;j++){
			if(2*j+i-2==n&&j>n1){ //乘法用2*,不能直接在数字前加个2
				n1=n3=j;
				n2=n+2-2*n1;
			}
		}
	}

//	n1=n3=n/3+1;  //有问题。。。比如goodstudy
//	n2=n/3+n%3;
}

int main()
{
	char a[1000];
	//freopen("G:\\in.txt","r",stdin);
	while(scanf("%s",a)!=EOF){
		int i,j,len=strlen(a);
		cntN1(len); //执行完后已计算出n1,n2,n3;
		for(i=0;i<n1;i++){
			printf("%c",a[i]);
			if(i!=n1-1){
				for(j=0;j<n2-2;j++)
					printf(" ");
			}else{
				for(j=n1;j<n1+n2-2;j++)
					printf("%c",a[j]);
			}
			printf("%c\n",a[len-1-i]);
		}
	}
	return 0;
}


 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页