当前位置:网站首页>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 HelloMethod 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;
}边栏推荐
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- 2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透
- Ae/pr/fcpx super visual effects plug-in package fxfactory
- Day18 - basis of interface testing
- Chapter 1: King Shehan miscalculated
- Comments on flowable source code (37) asynchronous job processor
- Common text labels
- PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
- Sentinel source code analysis part II - sentinel dashboard console startup and configuration
- Today's work summary and plan: February 14, 2022
猜你喜欢

Typora charges, WTF? Still need support

CMD implements the language conversion of locale non Unicode programs

第一章:求同吗小数和s(d, n)

Chapter 1: find the factorial n of n!

5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes

Chapter 1: seek common? Decimal and S (D, n)

BUUCTF

Chapter 1: recursively find the factorial n of n!

Day18 - basis of interface testing

Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
随机推荐
Day11 ---- 我的页面, 用户信息获取修改与频道接口
第一章: 舍罕王失算
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
04 -- QT OpenGL two sets of shaders draw two triangles
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
Detailed explanation of shuttle unity interworking principle
Luogu-p1107 [bjwc2008] Lei Tao's kitten
The way to treat feelings
第一章:求同吗小数和s(d, n)
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
Chapter 1: extend the same code decimal sum s (D, n)
UE source code analysis: uccharactermovementcomponent - rootmotion
Day10 -- forced login, token refresh and JWT disable
Summary of 90 day learning materials and notes of Zhang Fei's actual electronic hardware engineer
Strict data sheet of new features of SQLite 3.37.0
Chapter 1: seek common? Decimal and S (D, n)
IPv6 experiment
原生表格-滚动-合并功能
Thinking about festivals