当前位置:网站首页>Pat grade B 1009 is ironic (20 points)
Pat grade B 1009 is ironic (20 points)
2022-07-03 19:39:00 【How far is it forever】
Give me an English sentence , Ask you to write a program , Reverse the order of all the words in the sentence .
Input format :
The test input contains a test case , The total length given in one line does not exceed 80 String . A string consists of several words and spaces , The words are made of English letters ( There is a distinction between case and case ) Composed string , Use... Between words 1 Separate the spaces , Type to make sure there are no extra spaces at the end of the sentence .
Output format :
The output of each test case takes up one line , Output the sentences in reverse order .
Examples :"> sample input :
Hello World Here I Come
sample output :
Come I Here World Hello
Method 1 ( Refer to Qingshen code )
because PAT It's a single point test , Therefore, this more concise method is produced , The box EOF To determine whether the word has been entered
It should be noted that when entering manually, press ctrl + z End procedure
#include <iostream>
using namespace std;
int main()
{
int num = 0;
char ans[90][90];
while (scanf("%s", ans[num]) != EOF) {
num++;
}
for (int i = num - 1; i >= 0; i--) {
printf("%s", ans[i]);
if (i > 0) cout << " ";
}
return 0;
}
Method 2 simulation
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
char ans[90][90];
int r = 0, h = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
ans[r][h++] = s[i];
}
else {
r++; h = 0;
}
}
for (int i = r; i >= 0; i--) {
cout << ans[i];
if (i != 0) cout << " ";
}
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string s, temp = "";
getline(cin, s);
vector<string> ans;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') temp += s[i];
else {
ans.push_back(temp);
temp = "";
}
}
ans.push_back(temp);
for (int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i];
if (i != 0) cout << " ";
}
return 0;
}
Method 3 Use c++ Medium istringstream
istringstream Class to execute C++ Style stream input operation
istringstream The original form of the constructor is as follows :
istringstream::istringstream(string str), Its function is from string object str Read characters in
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string s, word;
getline(cin, s);
vector<string>ans;
istringstream ss(s);
while (ss >> word) {
ans.push_back(word);
}
for (int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i];
if (i != 0) cout << " ";
}
return 0;
}
边栏推荐
- 2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
- 第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
- Today's work summary and plan: February 14, 2022
- Zhang Fei hardware 90 day learning notes - personal records on day 4, please see my personal profile / homepage for the complete
- February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
- Zhang Fei hardware 90 day learning notes - personal records on day 2, please see my personal profile / homepage for the complete
- P1891 crazy LCM (Euler function)
- 2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
- 2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
- 5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
猜你喜欢
Xctf attack and defense world crypto advanced area best_ rsa
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
Chapter 1: recursively find the factorial n of n!
The necessity of lean production and management in sheet metal industry
01 - QT OpenGL display OpenGL window
Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
How to build an efficient information warehouse
2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
随机推荐
第二章:求a,b的最大公约与最小公倍数经典求解,求a,b的最大公约与最小公倍数常规求解,求n个正整数的的最大公约与最小公倍数
Chapter 1: King Shehan miscalculated
Day10 ---- 强制登录, token刷新与jwt禁用
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities
TFs and SVN [closed] - TFs vs SVN [closed]
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
Chapter 1: simplify the same code decimal sum s (D, n)
04 -- QT OpenGL two sets of shaders draw two triangles
What is the content of game modeling
01. Preparation for automated office (free guidance, only three steps)
Typora charges, WTF? Still need support
Summary of 90 day learning materials and notes of Zhang Fei's actual electronic hardware engineer
Detailed explanation of shuttle unity interworking principle
Floating source code comment (38) parallel job processor
Leetcode 1189. Maximum number of balloons (special character count)
第一章: 舍罕王失算
Chapter 1: recursively find the factorial n of n!
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]