string a;
stack<char> b;
int main() {
IOS;
int t;
scanf("%d",&t);
while (t--) {
getline(cin,a);
int cnt=0;
for(int i=0;i<=a.size();i++)
{
if(a[i]==' '||a[i]=='.'||a[i]=='!'||a[i]=='?')
cnt++;
}
for (int i = 0, j = a.size() - 2; i < j;i++,j-- ) {
while (a[i] != ' '&&cnt>=1) {
cout << a[i];
i++;
}
cnt--;
if(cnt>=1)
cout << " ";
while (a[j] != ' '&&cnt>=1) {
b.push(a[j]);
j--;
}
cnt--;
while (!b.empty()) {
cout << b.top();
b.pop();
}
if(cnt>=1)
cout << " ";
}
cout << a[a.size()-1]<<endl;
}
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: Visual Studio是我们用来编写C++程序的主要工具,我们先将它打开
C++中常见的算法包括但不限于以下几种:
算法用途:用于展示乘法口诀表
示例代码:
#include<iostream>
using namespace std;
int main(void)
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << i <<"*"<< j<<"="<< i* j<<" ";
}
cout << endl;
}
return 0;
}
算法用途:通过三目运算符实现简单的判断
示例代码:
int a = 9;
int b = 10;
int c = 0;
c = a > b ? a : b;
cout << c << endl;
算法用途:用于表达式的比较,并返回一个真值或假值
示例代码:
int a = 4;
int b = 3;
cout << (a < b) << endl;
算法用途:定义和使用结构体
示例代码:
struct Student
{
string name;
int age;
int score;
};
算法用途:统计三个人的总成绩
示例代码:
int score[3][3] = { {60,50,40},{10,20,30},{70,80,90} };
//嵌套循环解决
for (int i = 0; i < 3; i++)
{
int temp = 0;
for (int j = 0; j < 3; j++)
{
temp += score[i][j];
}
cout << temp << endl;
}
以上是C++中常见的算法及其用途和示例代码。如果还有其他算法需要了解,请告知。
```c++
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
std::string rearrangeSentence(std::string sentence) {
std::vector<std::string> words;
std::stringstream ss(sentence);
std::string word;
// 将句子按空格分割成单词列表
while (ss >> word) {
words.push_back(word);
}
// 提取标点符号
std::string punctuation = words.back();
// 去除标点符号后的单词列表
words.pop_back();
// 根据规则进行单词排序
std::vector<std::string> sortedWords;
for (int i = 0; i < words.size(); i++) {
int index = words[i][1] - '0' - 1;
sortedWords.push_back(std::string(1, words[i][index]));
}
// 组合成新的句子
std::string newSentence;
for (int i = 0; i < sortedWords.size(); i++) {
newSentence += sortedWords[i] + " ";
}
newSentence += punctuation;
return newSentence;
}
int main() {
int T;
std::cin >> T;
std::cin.ignore(); // 忽略换行符
std::vector<std::string> sentences;
for (int i = 0; i < T; i++) {
std::string sentence;
std::getline(std::cin, sentence);
sentences.push_back(sentence);
}
// 根据规则重新排序句子并输出结果
for (int i = 0; i < T; i++) {
std::string newSentence = rearrangeSentence(sentences[i]);
std::cout << newSentence << std::endl;
}
return 0;
}
```