当前位置:网站首页>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;
}
边栏推荐
- [wallpaper] (commercially available) 70 wallpaper HD free
- 2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透
- Chapter 1: sum of three factorials, graph point scanning
- Day10 -- forced login, token refresh and JWT disable
- Day11 - my page, user information acquisition, modification and channel interface
- JMeter connection database
- 第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
- How to build an efficient information warehouse
- Merge K ascending linked lists
- Part 28 supplement (XXVIII) busyindicator (waiting for elements)
猜你喜欢
Common text labels
第一章:求同吗小数和s(d, n)
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
I study database at station B (4): DQL
Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que
Chapter 1: recursively find the factorial n of n!
Today's work summary and plan: February 14, 2022
2020 intermediate financial management (escort class)
Chapter 1: simplify the same code decimal sum s (D, n)
2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP
随机推荐
QT -- qfile file read / write operation
Chapter 1: seek common? Decimal and S (D, n)
Professional interpretation | how to become an SQL developer
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
Use of aggregate functions
10 smart contract developer tools that miss and lose
The most valuable thing
What is the difference between a kill process and a close process- What are the differences between kill process and close process?
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
Day_ 18 IO stream system
Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析
QT -- qfileinfo file information reading
02 -- QT OpenGL drawing triangle
Find a line in a file and remove it
Summary of composition materials for 2020 high-frequency examination center of educational resources
NFT without IPFs and completely on the chain?
01 - QT OpenGL display OpenGL window
2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透
2022.2.14 Li Kou - daily question - single element in an ordered array