A1031. Hello World for U (20)/ 1993 problemB

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 n 1  characters, then 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 satisfied 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 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

题目大意:

输入一串字符长度为N(5<=N<=80),将字符输出为U型,包括两行一列,字符个数分别为n1、n2、n3;

由于u型图案拐角处有两个字符重合,n1+n2+n3 -2 =N; 题目要求图案越方越好,n1n3是不大于n2且满足n1+n2+n3=N+2的最大值

思路:

由题目知n1+n2+n3 -2 =N并且三者要尽可能接近(即图案越方越好); 即n1 = n3= (N + 2)/3(向下取整);再可求得n2;

借助二维数组存储数据,先均赋值为空格,根据题目要求输入相应元素,最后将二维数组输出;

代码如下:使用二维数组会更加直观;

#include 
#include 

int main() {
	char str[100], ans [40][40];
	gets (str);
	int n = strlen (str);
	int n1 = (n + 2) / 3, n3 = n1, n2 = n + 2 - 2 * n1;
	
	for (int i = 1; i <= n1; i++) {					//将ans中要用到的位置赋值空格; 
		for (int j = 1; j <= n2; j++) {
			ans[i][j] = ' ';
		}
	}
	
	int pos = 0;                                                    //从0开始遍历整个数组;
	for (int i = 1; i <= n1; i++) {
		ans[i][1] = str[pos++];					//先赋值第一列n1个字符; 
	} 
	
	for (int j = 2; j <= n2; j++) {
		ans[n1][j] = str[pos++];				//再赋值最后一行n2个字符; 
	}	
	
	for (int i = n3 -1; i >= 1; i--) {
		ans[i][n2] = str[pos++];				//最后赋值最后一列n3个字符,注意顺序自下而上; 
	}
	
	for (int i = 1; i <= n1; i++) {				
		for (int j = 1; j <= n2; j++) {
			printf ("%c", ans[i][j]);
		}
		printf ("\n");
	}
	
 	return 0;
}

还可直接将数组输出:

代码如下:

#include 
#include 

int main() {
	char str[100];
	gets (str);
	int n = strlen (str);
	int n1 = (n + 2) / 3, n3 = n1, n2 = n + 2 - 2 * n1;
	
	for (int i = 0; i < n1 -1 ; i++) {				
			printf ("%c", str[i]);					//输出前n1-1行第一个字符; 
			for (int j = 0; j < n2 - 2; j ++) {				
				printf (" ");						//输出空格; 
			}
	
			printf ("%c\n", str[n - i - 1]);		//输出该行最后一个字符; 
	}
	
	for (int i = 0; i < n2; i++) {                                   //输出n2个字符;
		printf ("%c", str[n1 + i - 1]); 			//字符串下标比字符位置小1;
	}	
		
	return 0;
}
	

codeup中采用多点测试,所以要用到while···EOF,

代码如下:

#include 
#include 

int main() {
	char str[100];
	
	while (scanf ("%s", str) != EOF) {                            //用于输入多组数据;
		int n = strlen (str);
		int n1 = (n + 2) / 3, n3 = n1, n2 = n + 2 - n1 - n3;
		
		for (int i = 0; i < n1 - 1; i++) {
			printf ("%c", str[i]);
			for (int j = 1; j < n2 -1; j++) {
				printf (" ");
			}
			printf ("%c\n", str[n - 1 - i]);
		}
		
		for (int i = 0; i < n2; i++) {
			printf ("%c", str[n1 + i - 1]);
		}
		printf ("\n");
	}

 	return 0;
}



你可能感兴趣的:(PAT,A,Code,up)