题解 | 多组_带空格的字符串_T组形式
多组_带空格的字符串_T组形式
https://www.nowcoder.com/practice/cff28a28d7f54419a640a8bb19f4275f
用C语言指针和C++的string函数实现
#include <iostream>
#include <string>
using namespace std;
char *del_space(char *str, int *space_num);
std::string delSpace(const std::string &str);
#include <algorithm>
int main() {
/*1.C语言
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
char str[1000000] = "";
scanf(" %[^\n]", str);
int space;
del_space(str, &space);
// reverse
n -= space;
int i = 0;
while (i < n / 2) {
str[i] = str[i] ^ str[n - 1 - i];
str[n - 1 - i] = str[i] ^ str[n - 1 - i];
str[i] = str[i] ^ str[n - 1 - i];
i++;
}
printf("%s\n", str);
}
*/
/*2.C++*/
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
char str_c[1000000] = "";
scanf(" %[^\n]", str_c);
std::string str = str_c;
std::string::size_type pos = 0;
while ((pos = str.find(' ')) != std::string::npos) {
str.erase(pos, 1);
}
reverse(str.begin(), str.end());
cout << str << endl;
}
}
// 64 位输出请用 printf("%lld")
char *del_space(char *str, int *space_num) {
char *q = str;
*space_num = 0;
int i = 0;
while (*q != '\0') {
if (*q != ' ') {
str[i] = *q;
i++;
q++;
} else {
(*space_num)++;
q++;
}
}
str[i] = '\0';
return str;
}

查看19道真题和解析