第一行有一个整数。
随后组数据。
每组的第一行有一个整数。
每组的第二行有一个字符串,仅包含小写英文字符和空格,保证字符串首尾都不是空格。
保证。
输出行,每行一个字符串,代表倒置后的字符串
。
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; }
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")
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) }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int k = 0; k < t; k++) { int n = sc.nextInt(); sc.nextLine(); String s = sc.nextLine().replace(" ", ""); StringBuilder reversedS = new StringBuilder(); for (int i = s.length()-1; i >= 0; i--) { reversedS.append(s.charAt(i)); } System.out.println(reversedS); } } }