首页 > 试题广场 >

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

[编程题]多组_带空格的字符串_T组形式
  • 热度指数:9113 时间限制: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.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)
#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;
}

发表于 2025-03-20 15:39:34 回复(0)
group = int(input())
for i in range(group):
    no = input()
    print(input().replace(' ', '')[::-1])

发表于 2024-12-04 12:12:50 回复(0)
字符串的题目与一般题目相比难度会有所提升,还得注意每个字符间的空格和换行符,我的解法不算是基础解法,所以我基本每句都写了注释,主要思路还是先用循环去除空格,再用循环将字符串的顺序颠倒,如果不熟悉指针,也可以不用函数,就直接在main函数中进行循环。
#include <stdio.h>
#include <string.h>// 使用字符串数据库
#define max_l 100005//定义最大测试长度
void a(char *str, char *newStr)//去除字符串中的空格
{
    int j = 0;// 定义一个存储空格数的值
    for (int i=0;str[i]!='\0';i++)// 循环检测空格
    {//若不为空格,则将其复制到newStr中,并递增j,间接去除了空格
        if (str[i]!=' ')
        {
            newStr[j]=str[i];
            j++;
        }
    }
    newStr[j]='\0';// 处理完毕,在末尾添加结束字符,得到不含空格的字符串
}
// 反转字符串
void b(char *str)
{
    int len=strlen(str);//通过strlen函数得到处理后的字符串的长度,从而确定循环次数
    for (int i=0;i<len/2;i++)//将前半部分与后半部分的字符互换,故只需要循环字符串长度的一半
    {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}
int main(void)
{
    int t;//定义需要测试的组数
    scanf("%d",&t);
    for (int k=0;k<t;k++)
    {
        int n;//定义输入带空格字符串的长度
        scanf("%d",&n);
        getchar();//读取输入结束后的换行符
        char l[max_l];//定义一个能存储最大长度字符串的数组
        fgets(l, max_l, stdin);//这里之所以不用n作为被读取字符串的长度,是为了统一fgets读取的长度,因为n不是固定不变的,会使计算内容变得麻烦,且用最大长度可避免输入字符的长度超过n的情况,避免出现崩溃的情况
        l[strcspn(l,"\n")]='\0';//将读取到的换行符换为结束符,从而达到分开每组数据的条件
        char nl[max_l];//定义一个数组,存储去除空格后的字符串
        a(l, nl);//进入去除字符串中空格的函数
        b(nl);//进入反转处理后的字符串的函数
        printf("%s\n", nl);//输出结果
    }
    return 0;
}
发表于 2025-02-24 20:57:24 回复(0)
import sys
t = int(sys.stdin.readline().strip())
for _ in range(t):
    n = int(sys.stdin.readline().strip())
    s = sys.stdin.readline().strip()
    s = s.replace(" ","")
    reversed_s = reversed(s)
    s = "".join(reversed_s)
    print(s)

发表于 2025-04-21 14:48:25 回复(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());
        }

发表于 2025-04-19 10:47:00 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        getchar();                  // 读取回车符
        string s="";
        getline(cin,s);             // 按行读取
        for(int i=n-1;i>=0;i--)
            if(s[i]!=' ')
                cout<<s[i];
        cout<<endl;
    }
}

发表于 2025-04-18 21:51:21 回复(0)
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()));
        }

    }
}

发表于 2025-04-06 14:58:15 回复(0)
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)
t = int(input())
for i in range(t):
    tmp = int(input())
    s = list(map(str,input().strip().split()))
    # print(s)
    # print(s[::-1])
    result = ''
    for row in s[::-1]:
        result += row[::-1]
    print(result)

发表于 2025-03-20 10:00:35 回复(0)

#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")

发表于 2025-03-19 16:19:37 回复(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)
}

发表于 2025-03-17 22:03:44 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    long int t;
    scanf("%ld", &t);
    getchar();
    long int n;
    for(long int i=0; i<t; i++){
        scanf("%ld", &n);
        getchar();
        char s[n];
        for (long int j=0; j<n; j++) {
            scanf("%c", &s[j]);
        }
        getchar();
        for(long int j=n-1; j>=0; j--){
            if(s[j]!=' '){
                printf("%c", s[j]);
            }
        }
        printf("\n");
    }
    return 0;
}
发表于 2025-03-17 19:28:40 回复(0)
#include <stdio.h>

int main() {

    long long int t=0,n=0;

    scanf("%lld",&t);
    for (int i = 0; i < t ; i++) {
        scanf("%lld",&n);
        char str[n];
        scanf(" %[^\n]",str);//用于跳过输入缓冲区中的空白字符(如空格、制表符、换行符等)

        for (int j = n-1; j>=0; j--) {
            if(str[j] != ' ')
            {
                printf("%c",str[j]);
            }
        }
        printf("\n");
    }
   

    return 0;
}
发表于 2025-02-12 11:39:37 回复(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)
#include<bits/stdc++.h>
  char a[100000];
using namespace std;
int main ()
{
int t;
cin>>t;
int n;
for(int i=0;i<t;i++)
{
cin>>n;
for(int j=0;j<n;j++)
{
 
scanf("%c",&a[j]);
     
}
for(int y=n-1;y>=0;y--)
{
   if(a[y]==' ')
    continue;

cout<<a[y];
}
cout<<"\n";
}
    return 0;
}
谁能帮我看看为什么只输出了一行
发表于 2024-12-09 20:01:13 回复(0)
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);
        }
    }
}

发表于 2024-12-07 20:50:03 回复(0)
t = int(input())
for _ in range(t):
    n = int(input())
    str_list = list(input().split())
    pre = ""
    for i in range(len(str_list)):
        pre = pre + str_list[i]

    result = "".join(reversed(pre))
    print(result)

发表于 2024-12-07 19:36:40 回复(0)
t = int(input())

for i in range(t):
    n_char = int(input())
    str_char = input().replace(" ", "")
    print("".join(reversed(str_char)))

发表于 2024-11-13 07:32:32 回复(0)
import sys
con=0
list=[]
for line in sys.stdin:
    a = line.strip()
    con+=1
    if con==1:
        continue
    elif con%2==0:
        continue
    else:
        list=a[::-1]
        print(list.replace(" ",""))

发表于 2024-11-01 11:39:17 回复(0)