490 - Rotating Sentences

题意: 
将输入的N行(<=100)字符串左转90度进行输出.

思路:
将所有输入保存到 vector 中, 然后从最后一行开始, 逐列进行输出, 输出的总列数为最长字符串的长度.

要点:
1. 由于输入列中有空格, 所以不能使用 cin 进行读入, 应使用 while( getline(cin, str) ) 这样的形式. 不要使用 while( !cin.eof()) { getline(cin, str); }, 不然每次都会多读入一个空行.
2. 当某一行字符串对应列没有字符时, 应输出空格, 而不是空白字符('\0');

题目:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=431

代码:

# include <iostream>
# include <string>
# include <vector>
using namespace std;

// 要注意两点: 
// 1. 使用 getline 来读入一整行
// 2. 长度不够时, 输出一个空格' ', 而不是空字符 '\0'
char getChar(string str, int index){
	if( index >= str.length() ){
		return ' ';		// \0 代表空字符
	}else{
		return str[index];
	}
}

int main(int argc, char const *argv[])
{	
	vector<string> input;
	string str;

	int maxLength = 0;
	while( getline(cin, str) ){
		input.push_back(str);
		maxLength = (str.length() > maxLength ? str.length() : maxLength);
	}

	for(int i=0; i<maxLength; i++){
		for(int j=input.size()-1; j>=0; j--){		
			cout << getChar(input[j], i);
		}
		cout << endl;
	}

	return 0;
}

环境:C++ 4.5.3 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE

你可能感兴趣的:(uva,rotating,490,Sentences)