补题:C - Text Reverse
Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3
olleh !dlrow
m’I morf .udh
I ekil .mca
**
Sample Output
**
hello world!
I’m from hdu.
I like acm.
[hint]
Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.
[/hint]
题后总结
首先要梳理一下关于栈的基本知识点。
栈:基本的数据结构之一,特点是“先进后出”。
这道题现在来看是没有什么难度的,最主要的还是对栈的理解还停留到简单的i know层面,并没有从真正的掌握其用法,学会了只代表你学会了,和会用是另外一回事。
#include <bits/stdc++.h>//万能头文件
using namespace std;
int main()
{
int n;
char ch;
cin>>n;
getchar();
while(n--){
stack<char>s;
while(true){
ch=getchar();//一次读入一个字符
if(ch==' '||ch=='\n'||ch==EOF){
while(!s.empty()){
cout<<s.top();//出栈
s.pop();//清楚栈顶
}
if(ch=='\n'||ch==EOF) break;cout<<" ";
}
else s.push(ch);//入栈
}
cout<<"\n";
}
return 0;
}
c++中几个输出的差异
1.cin>>
接收单个数字、字符or接受一个字符数组,遇“空格”、“Tab”、“回车”结束,后面的字符不在读取。
2. cin.get()
一:char ch=cin.get();
a可以是任何字符: 数字,字母,符号,空格,回车,tab
//二:cin.get(字符数组名,接收字符数)用来接收一行字符串,可以接收空格
#include<iostream>
using namespace std;
int main(){
char c[20];
cin.get(c,20);//数组被填满,或者遇回车结束,可以读入空格等除回车之外的其他字符
return 0;
}
3. cin.getline()
与cin.get()的功能二基本相同
char c[20];
cin.getline(c,20);//数组被填满,或者遇回车结束,可以读入空格等除回车之外的其他字符
4.getline()
包含在库`#include<string>`中
string str;
getline(cin,str);遇回车结束,可以读入空格等除回车之外的其他字符
#include<iostream>//在比赛中直接用真相的万能头文件,省时省力
#include<string>
using namespace std;
void main()
{
string str;
getline(cin, str);
cout << str << endl;
}
5.gets()
头文件:stdio.h(c),cstdio(C++)
gets从标准输入设备读字符串函数,其可以无限读取,不会判断上限,以回车结束读取
(参考:https://baike.baidu.com/item/gets/787649?fr=aladdin)
#include <iostream>//在比赛中直接用真相的万能头文件,省时省力
#include <cstdio>
using namespace std;
int main(){
char str[100];
gets(str);
cout<<str<<endl;
return 0;
}//gets_s函数用法基本一样,此处略
6.getchar()
getchar()//接受任何一个字符,包括空格、回车、tab键。
#include<iostream>
using namespace std;
void main()
{
char ch;
ch = getchar(); //不能写成getchar(ch);
cout << ch << endl;
}