【PAT】1031.ハローワールドfor U(20)

1788 ワード

タイトル:http://pat.zju.edu.cn/contests/pat-a-practise/1031
分析:
簡単な問題です.配列の出力順序を調整すればいいです.
入力配列長はlenです.n 1=n 3=(len+2)/3   n 2=len-n 1-n 3
最初の行の一番左は元の配列の最初の文字で、最後の端は元の配列の最後の文字です.全部でn 1行を出力します.
テーマの説明:
Ginven any string of N(>=5)characters,you are asked to form the characters into the shop of U.For example,「ハロルド」can be presd as:
h  d
e  l
l  r
lowo
That is、the characters must be printesd in the order、starting top-down from the left vertical line with n
1 characters,the n left to right along the bottom line with n
2 characters,and finally bottom-up along the vertical line with n
3 characters.And more,we would like U to be as squared as possible--that is,it must be satis fied that n
1=n
3=max{k|k<=n
2 for all 3<=n
2<=N}with n
1+n
2+n
3-2=N.
Input Specification:
Each input file contains one test case.Each case contains one string withのless than 5 andのmore than 80 characters in a line.The string containsの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
参照コード:
#include<iostream>
#include<string.h>
using namespace std;
#define max 85
char input[max];

int main()
{
	int len;
    int n1,n2,n3;
	int i,j;
	cin>>input;
	len = strlen(input);
	n1 = n3 = (len + 2) / 3 ;
	n2 = len - 2*n1;
    for(i=0; i<n1-1; i++)
	{
		cout<<input[i];
		for(j=0; j<n2; j++) cout<<" ";
		cout<<input[len-1-i]<<endl;
	}
	for(j=i; j<len-i; j++)
		cout<<input[j];
	cout<<endl;


	return 0;
}