第一行有一个整数。
随后组数据。
每组的第一行有一个整数。
每组的第二行有一个字符串,仅包含小写英文字符和空格,保证字符串首尾都不是空格。
保证。
输出行,每行一个字符串,代表倒置后的字符串
。
3 9 one space 11 two spaces 14 three spaces
ecapseno secapsowt secapseerht
#include <iostream>
#include <string>
using namespace std;
int main() {
int t;
cin >> t;
cin.ignore(); // 清除输入缓冲区的换行符
while (t--) {
int n;
cin >> n;
cin.ignore(); // 清除输入n后的换行符
string s;
// 读取n个字符(包括空格)
for (int i = 0; i < n; ++i) {
char c;
cin.get(c); // 逐字符读取,包括空格
s.push_back(c);
}
// 逆序输出非空格字符
for (int i = n - 1; i >= 0; --i) {
if (s[i] != ' ') {
cout << s[i];
}
}
cout << endl;
}
return 0;
} package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strconv"
)
func main() {
reader := bufio.NewReader(os.Stdin)
var buf bytes.Buffer
// 读取测试用例数量 t
t, _ := strconv.Atoi(readLine(reader))
for i := 0; i < t; i++ {
// 读取每组数据的 n
_, _ = strconv.Atoi(readLine(reader))
// 读取字符串 s
s := readLine(reader)
// 移除字符串中的空格
s = removeSpaces(s)
// 倒置字符串
reversed := reverseString(s)
// 缓存输出结果
buf.WriteString(reversed)
buf.WriteString("\n")
}
// 一次性输出所有结果
fmt.Print(buf.String())
}
// 读取一行输入
func readLine(reader *bufio.Reader) string {
line, _ := reader.ReadString('\n')
return line[:len(line)-1] // 去掉换行符
}
// 移除字符串中的空格
func removeSpaces(s string) string {
return strings.ReplaceAll(s, " ", "")
}
// 倒置字符串
func reverseString(s string) string {
bytes := []byte(s)
for left, right := 0, len(bytes)-1; left < right; left, right = left+1, right-1 {
bytes[left], bytes[right] = bytes[right], bytes[left]
}
return string(bytes)
} #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
string removeSpaces(const string& s) {
string result;
for (char c : s) {
if (c != ' ') {
result += c;
}
}
return result;
}
int main() {
int a, b;
cin >> a;
vector<string> result;
while (a--) {
cin >> b;
string s = "";
//使用 cin >> d 读取字符时,会跳过空白字符(包括空格和换行符)
cin.ignore();
getline(cin, s);
string noSpaceStr = removeSpaces(s);
reverse(noSpaceStr.begin(), noSpaceStr.end());
result.push_back(noSpaceStr);
noSpaceStr = "";
}
for (auto & j : result){
cout << j << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld") package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 1024*1024), 1024*1024) // 增加缓冲区大小
sc.Scan()
t, _ := strconv.Atoi(sc.Text())
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
for i := 0; i < t; i++ {
sc.Scan()
_ = sc.Text() // 读取n,但不需要使用
sc.Scan()
s := sc.Text()
// 预分配足够的内存
result := make([]byte, 0, len(s))
// 从后向前遍历,跳过空格
for j := len(s) - 1; j >= 0; j-- {
if s[j] != ' ' {
result = append(result, s[j])
}
}
writer.WriteString(string(result))
writer.WriteByte('\n')
}
}
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 1024*1024), 1024*1024)
sc.Scan()
t, _ := strconv.Atoi(sc.Text())
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
for i := 0; i < t; i++ {
sc.Scan()
_ = sc.Text()
sc.Scan()
s := sc.Text()
// 计算非空格字符的数量
count := 0
for j := 0; j < len(s); j++ {
if s[j] != ' ' {
count++
}
}
// 直接构建结果
result := make([]byte, count)
idx := 0
for j := len(s) - 1; j >= 0; j-- {
if s[j] != ' ' {
result[idx] = s[j]
idx++
}
}
writer.Write(result)
writer.WriteByte('\n')
}
} int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
in.nextLine();
String s = in.nextLine().replace(" ","");
System.out.println(new StringBuffer(s).reverse().toString());
} using System.Linq;
public class Program {
public static void Main() {
string str = string.Empty;
//获取组数
int t = int.Parse(System.Console.ReadLine ());
//控制读取
for (int i = 0; i < t; i++) {
System.Console.ReadLine ();
//去掉空格
str = System.Console.ReadLine().Trim();
str = str.Replace(" ", "");
//倒置输出
System.Console.WriteLine(new string(str.Reverse().ToArray()));
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = 0;
if(in.hasNextInt()){
t = in.nextInt();
}
int n = 0;
while(0<t--){
if(in.hasNextInt()){
n = in.nextInt();
}
// 消除空行
in.nextLine();
StringBuilder sb = new StringBuilder();
if (in.hasNextLine()){
sb.append(in.nextLine().replaceAll(" ",""));
}
System.out.println(sb.reverse());
}
in.close();
}
} #include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
string s, sum;
while(cin >> s){
reverse(s.begin(), s.end());
sum.insert(0, s);
if(cin.get()=='\n'){
break;
}
}
cout << sum << endl;
}
}
// 64 位输出请用 printf("%lld")