codeforce 5B Center Alignment
2014-05-15 22:35
351 查看
B. Center Alignment
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.
You are to implement the alignment in the shortest possible time. Good luck!
Input
The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total
amount of the lines do not exceed 1000.
Output
Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the
line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample
tests carefully to understand the output format better.
题目大意是给一些字符串,然后依次将他们居中打印出来。然后用一个最小的星星围成的矩形把这些包围起来。当不能恰好居中的时候比较麻烦,需要先中靠左输出,下次遇到不能恰好居中的就中偏右输出,交替进行。我会告诉你我这题交了7次么,瞎了。
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.
You are to implement the alignment in the shortest possible time. Good luck!
Input
The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total
amount of the lines do not exceed 1000.
Output
Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the
line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample
tests carefully to understand the output format better.
题目大意是给一些字符串,然后依次将他们居中打印出来。然后用一个最小的星星围成的矩形把这些包围起来。当不能恰好居中的时候比较麻烦,需要先中靠左输出,下次遇到不能恰好居中的就中偏右输出,交替进行。我会告诉你我这题交了7次么,瞎了。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <stack> #include <map> #include <queue> #include <algorithm> using namespace std; void putc(int n, char c) { while (n--) cout << c; } int main() { string str[1005]; char ctmp[1005]; int cnt = 0; int longest = 0; while (cin.getline(ctmp, 1005)) { str[cnt] = ctmp; if (str[cnt].length() > longest) longest = str[cnt].length(); cnt ++; } int err = 0; int left, right; putc(longest+2, '*'); cout << endl; for (int i = 0; i < cnt; i++) { left = (longest - str[i].length()) / 2; right = longest - left - str[i].length(); if (left != right) { left += (err % 2 == 0 ? 0 : 1); right -= (err % 2 == 0 ? 0 : 1); err++; } cout << '*'; putc(left, ' '); cout << str[i]; putc(right, ' '); cout << '*' << endl; } putc(longest+2, '*'); cout << endl; return 0; }
相关文章推荐
- CodeForce 681C 模拟题
- CodeForce Round #186 Div.2
- 5C-codeforce Longest Regular Bracket Sequence
- codeforce 192 div2解题报告
- CodeForce 614B Gena's Code(水题)
- codeforce 777 C. Alyona and Spreadsheet (打表预处理)
- Codeforce - 361 - B. Levko and Permutation
- ACM刷题之codeforce————Race Against Time
- Codeforce 705D 贪心选择
- codeforce Gym 100500E IBM Chill Zone (SG函数)
- CodeForce334A
- codeforce 329B Biridian Forest(bfs)
- codeforce 148->A. Insomnia cure
- codeforce 437D 并查集+贪心
- Codeforce 567D
- codeforce 510 C 拓扑排序
- T-shirt buying Codeforce 贪心
- CodeForce #318 div 2 C Bear and Poker
- CodeForce 2A Winner
- codeforce 551C GukiZ hates Boxes(二分+贪心)