完美世界笔试题。反转数字
#include <iostream>
#include <string>
using namespace std;
int main() {
char str[10000] = {'0'};
gets_s(str);
int len = strlen(str);
int i = 0, j = 0;
//删除多余空格
while (j < len) {
while (j < len && str[j] == ' ') j++;
while (j < len && str[j] != ' ' ) {
str[i++] = str[j++];
}
if (i < len) str[i++] = ' ';
else break;
}
while(str[--i] == ' ');
str[++i] = '\0';
len = i;
//反转
i = 0, j = len - 1;
while (i < j) {
char temp = str[i];
str[i++] = str[j];
str[j--] = temp;
}
//数字反转
i = 0, j = 0;
while (j < len) {
while (j < len && str[j] != ' ')j++;
j--;
while (i < j) {
char temp = str[i];
str[i++] = str[j];
str[j--] = temp;
}
while (i < len && str[i] != ' ')i++; i++;
while (j < len && str[j] != ' ')j++; j++;
}
cout << str << endl;
system("pause");
return 0;
} #C++工程师#
realme公司福利 335人发布