Hello World for U

2012年浙江大学计算机及软件工程研究生机试真题

考察对字符串的操作。

#include <stdio.h>
#include <string.h>
char str[81];

int main(){
	int len, n1, n2, n3, i, j, count;
	while(scanf("%s", str) == 1){
		len = strlen(str);
		n1 = n3 = len / 3;
		n2 = len - n1 - n3;
		if(n1 == n2){
			--n1; --n3;
			n2 += 2;
		}
		count = 0;
		for(i = 1; i <= n1; ++i){
			for(j = 1; j <= n2; ++j)
				if(j == 1) putchar(str[count++]);
				else if(j == n2) putchar(str[len - count]);
				else putchar(' ');
			puts("");
		}
		//输出最后一行
		for(j = 1; j <= n2; ++j)
			putchar(str[count++]);
		puts("");
	}
	return 0;
}


posted on 2014-02-19 14:54  长木Qiu  阅读(129)  评论(0编辑  收藏  举报