TJU Problem 1644 Reverse Text

注意:

  int N; cin >> N; cin.ignore();

  同于

  int N; scanf("%d\n",&N);

另:关于 cin 与 scanf:

scanf是格式化输入,printf是格式化输出。
cin是输入流,cout是输出流。效率稍低,但书写简便。
格式化输出效率比较高,但是写代码麻烦。
流输出操作效率稍低,但书写简便。
cout之所以效率低,正如一楼所说,是先把要输出的东西存入缓冲区,再输出,导致效率降低。

缓冲区比较抽象,举个例子吧:
曾经就遇到过这样的情况(类似的),
int i;
cout<<‘a‘;
cin>>i;
cout<<‘b‘;
运行结果什么都没看到输出,输入一个整型比如3再按回车后ab同时显示出来了。
但是这样的情况并不是经常发生,是在一些比较大型的工程中偶尔出现,原因是字符a先到了缓冲区,但是没输出,等输入了i,b进入
缓冲区后再一并输出的。
流输入也是差不多的。

cin的实时性较差,因为它使用了缓冲区,一般情况下满了才刷新的。

对于字符:cin的输入忽略空格和回车。scanf("%c",&i)等价于i = getchar(),换行符和回车都会被读入。

原题:

1644.   Reverse Text


Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 7899   Accepted Runs: 2891



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

Source: Western
and Southwestern European Regionals 1996 Practice

源代码:

我的:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <stdio.h>
 4 using namespace std;
 5
 6 char aaa[75];
 7
 8 int main()    {
 9     int N;    cin >> N;    cin.ignore();
10     while (N--)    {
11         gets(aaa);
12         int len = strlen(aaa);
13         for (int i = len - 1; i >= 0; i--)    {
14             cout << aaa[i];
15         }
16         cout << endl;
17     }
18     return 0;
19 }

网上的:

  注意学习其中 reverse 函数:

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 int main()    {
 7
 8   int cases;
 9   cin >> cases;
10   string s;
11   getline(cin, s);
12   while (cases-- && getline(cin, s))    {
13     reverse(s.begin(), s.end());
14     cout<< s << endl;
15   }
16   return 0;
17 }
时间: 2024-10-09 22:47:22

TJU Problem 1644 Reverse Text的相关文章

HDU1321 ZOJ1295 Reverse Text

问题链接:HDU1321 ZOJ1295 Reverse Text.基础训练级的题,用C语言编写. 这个问题是首先输入测试例子数量t,然后输入t行字符串,将每一行逆序输出. 利用堆栈后进先出的原理,逆序处理可以使用堆栈来实现. 程序中,使用了一个自行实现的堆栈,简单地实现逆序功能. 本程序使用getchar()函数处理输入流,除了输入字符压栈外,读入的字符直接输出输出,没有使用多余的缓存. 这个问题类似于HDU1062 Text Reverse. AC通过的C语言程序如下: /* HDU1321

Problem Evaluate Reverse Polish Notation

Problem Description:  Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Solution: 1 public int evalRPN(String[] tokens) { 2 Stack<String>

zju 1295 Reverse Text

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 automat

HDOJ/HDU 1321 Reverse Text(倒序输出~)

Problem Description 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 lang

TJU Problem 1065 Factorial

注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065.   Factorial Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 6067   Accepted Runs: 2679 The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceiver

Leet code problem 7: reverse integer digit

class Solution { public: int reverse(int x) { int ret = 0; int int_max_divide_10 = INT_MAX / 10; int int_max_mod_10 = INT_MAX % 10; int int_min_divide_10 = INT_MIN / 10; int int_min_mod_10 = INT_MIN % 10; while (x != 0){ int remainder = x % 10; if (x

TJU Problem 2520 Quicksum

注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520.   Quicksum Time Limit: 0.5 Seconds   Memory Limit: 65536KTotal Runs: 2964   Accepted Runs: 1970 A checksum is an algorithm that scans a packet of data and returns a single

TJU Problem 1015 Gridland

最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 1 做这题时,千万不要被那个图给吓着了,其实这题就是道简单的数学题. 2 3 首先看当m或n中有一个为2的情况,显然,只需要算周长就OK了.即(m+n-2)*2,考虑到至少其中一个为2,所以答案为2 *m或2*n,亦即m*n.注意这里保证了其中一个数位偶数. 4 当m,n≥3时,考虑至少其中一个为偶数的情况,显然,这种情况很简单,可以得出,结果为m*

TJU Problem 2857 Digit Sorting

原题: 2857.   Digit Sorting Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3234   Accepted Runs: 1704 Several players play a game. Each player chooses a certain number, writes it down (in decimal notation, without leading zeroes) and sorts t