题目链接
#include<iostream>
#include<string>
using namespace std;
void swap(char &a, char &b) { // 交换两个字符串
char tmp = a;
a = b;
b = tmp;
}
int main() {
int n;
cin >> n;
string s;
while (n--) {
cin >> s;
for (int i = 0; i < s.size() - 1; i += 2) { // 在s字符串上原地修改
swap(s[i], s[i + 1]);
}
cout << s << endl;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while((str = reader.readLine())!= null){
StringTokenizer tokenizer = new StringTokenizer(str);
int n = Integer.parseInt(tokenizer.nextToken());
for(int i = 0; i < n; i++){
String s = reader.readLine();
StringBuilder sb = new StringBuilder();
for(int j = 0; j < s.length(); j+= 2){
sb.append(s.charAt(j +1)).append(s.charAt(j));
}
System.out.println(sb.toString());
}
}
}
}
// 方法二:原地交换
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String s1 = sc.nextLine();
int len = s1.length();
char[] chs = s1.toCharArray();
for (int j = 0; j < len - 1; j += 2) {
swap(chs, j, j + 1);
}
System.out.println(new String(chs));
}
sc.close();
}
private static void swap(char[] chs, int j, int i) {
char tmp = chs[j];
chs[j] = chs[i];
chs[i] = tmp;
}
}
C = int(input())
for _ in range(C):
s = input()
even_chars = s[1::2]
odd_chars = s[::2]
result = ''.join(e + o for e, o in zip(even_chars, odd_chars))
print(result)
#include <stdio.h>
#include <string.h>
void swap(char* a, char* b) { // 交换两个字符
char tmp = *a;
*a = *b;
*b = tmp;
}
int main() {
int n;
scanf("%d", &n);
char s[100];
while (n--) {
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len - 1; i += 2) { // 在s字符串上原地修改
swap(&s[i], &s[i + 1]);
}
printf("%s\n", s);
}
return 0;
}