您的位置:首页 > 编程语言

string的使用uva-1593 代码对齐

2017-04-23 10:41 369 查看
收藏入string类吧,

A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, · · · , an),the next n-tuple in the sequence is formed by taking the absolute differences of neighboring
integers:(a1, a2, · · · , an) → (|a1 − a2|, |a2 − a3|, · · · , |an − a1|)Ducci sequences either reach a tuple of zeros or fall into a periodic loop. For example, the 4-tuplesequence starting with 8,11,2,7 takes 5 steps to reach the zeros tuple:(8, 11, 2, 7)
→ (3, 9, 5, 1) → (6, 4, 4, 2) → (2, 0, 2, 4) → (2, 2, 2, 2) → (0, 0, 0, 0).The 5-tuple sequence starting with 4,2,0,2,0 enters a loop after 2 steps:(4, 2, 0, 2, 0) → (2, 2, 2, 2, 4) → (0,0,0,2,2) → (0, 0, 2, 0, 2) → (0, 2, 2, 2, 2) → (2, 0, 0, 0, 2) →(2, 0,
0, 2, 0) → (2, 0, 2, 2, 2) → (2, 2, 0, 0, 0) → (0, 2, 0, 0, 2) → (2, 2, 0, 2, 2) → (0, 2, 2, 0, 0) →(2, 0, 2, 0, 0) → (2, 2, 2, 0, 2) → (0, 0, 2, 2, 0) → (0, 2, 0, 2, 0) → (2, 2, 2, 2, 0) → (0,0,0,2,2) → · · ·Given an n-tuple of integers, write a program to
decide if the sequence is reaching to a zeros tupleor a periodic loop.

Input

Your program is to read the input from standard input.input
consists of T test cases. The numberof test cases T is given in the first line of the input. Each test case starts with a line containing aninteger n (3 ≤ n ≤ 15), which represents the size of a tuple in the Ducci sequences. In the followingline, n integers
are given which represents the n-tuple of integers. The range of integers are from 0 to1,000. You may assume that the maximum number of steps of a Ducci sequence reaching zeros tupleor making a loop does not exceed 1,000.、

Output

Your program is to write to standard output. Print exactly one line for each test case. Print ‘LOOP’ ifthe Ducci sequence falls into a periodic loop, print ‘ZERO’ if the Ducci
sequence reaches to a zeros tuple.

Sample Input

4

4

8 11 2 7

5

4 2 0 2 0

7

0 0 0 0 0 0 0

6

1 2 3 1 2 3

Sample Output

ZERO

LOOP

ZERO

LOOP

/*****************************************************************************/

题意如下:

输入若干行代码,要求各列单词的左边界对齐且尽量靠左。

单词之间至少要空一格。每个单词不超过80个字符,

每行不超过180个字符,一共最多1000行。

 

注意输出时每行的最后一列后面没有空格符。

 

代码如下:代码中有详细的注释!

[html] view
plain copy

<pre name="code" class="cpp">#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
vector<string> txt[1314];
string code,te;
int max_len[250];//将每一列中最长的单词的长度保存下来,以便保证格式!
void print(string s,int len)
{//格式化输出!
for(int i=0; i<s.size(); i++)
cout<<s[i];
for(int i=0; i<=len-s.size(); i++)
cout<<' ';
//cout<<'*';
}
int main()
{
int col=0,row=0;//代码行数,col表示每一行的“单词”个数!
while(getline(cin,code))
{
stringstream tran(code);//创建一个“字符串流”——tran,接下来只需要像读取cin那样读取tran即可!
while(tran>>te)
{
max_len[col]=max(max_len[col],(int)te.size());//比较长度取大值。
col++;
txt[row].push_back(te);//将每一个单词存进容器里面!
}
row++,col=0;
}
for(int i=0; i<row; i++)
{
int j=0;
for(; j<txt[i].size()-1; j++)
print(txt[i][j],max_len[j]);
cout<<txt[i][j]<<endl;//每行的最后一列是不用输出空格的!
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: