现在的位置: 首页 > 算法 > 正文

zoj1295

2018年05月09日 算法 ⁄ 共 1349字 ⁄ 字号 评论关闭

Reverse Text


Time Limit: 2 Seconds     
Memory Limit:
65536 KB


In most languages, text is written from left to right. However, there are other languages where text is read and written from right to left. As a first step towards a program that automatically translates from a left-to-right language into a right-to-left
language and back, you are to write a program that changes the direction of a given text.


Input Specification

The input contains several test cases. The first line contains an integer specifying the number of test cases. Each test case consists of a single line of text which contains at most 70 characters. However, the newline character at the end of each line is
not considered to be part of the line.

Output Specification

For each test case, print a line containing the characters of the input line in reverse order.

Sample Input

3
Frankly, I don't think we'll make much
money out of this scheme.
madam I'm adam

Sample Output

hcum ekam ll'ew kniht t'nod I ,ylknarF
.emehcs siht fo tuo yenom
mada m'I madam

  这是一个字符串反转的问题,可以直接调用STL 里的reverse 函数。 但是,需要注意读入的问题,getline 函数默认以换行符'\n'终止, 但是得注意输入test case 数量 N 后边有个回车换行被getline接受了,导致第一个接受的是回车换行而非字符串,这也是很多童鞋输出时多了一行空白少了一行字符串的原因 。 这里一定要注意。

  我们可以读入N+1个字符串,输出的时候省略第一个读入的空行,从第二行开始输出就满足题目要求了。

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;

main()
{
// ifstream cin("a.txt");
 int n;
 cin>>n;
 n++;
 int k=0;
 while(n--)
 { 
  string s;
  getline(cin,s);
  if(k)  // k不为0时,if里边为真,执行语句
  {
  reverse(s.begin(),s.end());
  cout<<s<<endl; }
  k++;
 }
return 0;
}

 

 

 

 

抱歉!评论已关闭.