首页 > 试题广场 >

多组_带空格的字符串_T组形式

[编程题]多组_带空格的字符串_T组形式
  • 热度指数:9232 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定 t 组询问,每次给出一个长度为 n 的带空格的字符串 s ,请你去掉空格之后,将其倒置,然后输出。

输入描述:
第一行有一个整数 t\ (\ 1 \leq t \leq 10^5\ )
随后 t 组数据。
每组的第一行有一个整数 n\ (\ 1 \leq n \leq 10^5\ )
每组的第二行有一个字符串 s,仅包含小写英文字符和空格,保证字符串首尾都不是空格。
保证 \sum n \leq 10^5


输出描述:
输出 t 行,每行一个字符串,代表倒置后的字符串 s
示例1

输入

3
9
one space
11
two  spaces
14
three   spaces

输出

ecapseno
secapsowt
secapseerht
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();
    }
}

发表于 2025-04-05 23:12:06 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int group = in.nextInt();
        for (int i = 0; i < group; i++) {
            int n =  in.nextInt();
            in.nextLine(); // 去除换行符
            String s = in.nextLine();
            s = s.replaceAll(" ", "");
            System.out.println(new StringBuilder(s).reverse().toString());
        }
    }
}
发表于 2024-12-31 15:38:25 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //t组数据
        // int t = in.nextInt();
        String t = in.nextLine();
        int i = 0;
        while (i++ < Integer.parseInt(t)) {
            //整数n
            // int n = in.nextInt();
            String n = in.nextLine();
            //替换所有的空格
            StringBuilder s =
            new StringBuilder(in.nextLine().replaceAll(" ",""));
            System.out.println(s.reverse());
        }
    }
}
发表于 2024-09-26 17:48:37 回复(0)