完美世界笔试题。反转数字

#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++工程师#
全部评论
我直接用vector存储输入然后逆序输出了。。。
点赞 回复 分享
发布于 2017-09-27 00:12
我也是这个,血崩
点赞 回复 分享
发布于 2017-09-26 23:10
#include <iostream> #include <vector> #include <string> using namespace std; int main() {     vector<string> s;     string in;     while (cin >> in)     {         s.insert(s.begin(), in);     }     for (vector<string>::const_iterator i = s.begin(); i != s.end(); i++)     {         cout << *i;         if (i+1 != s.end())         {             cout << " ";         }     }     system("pause");     return 0; }
点赞 回复 分享
发布于 2017-09-26 23:03
然而笔试并没有做出来。。哎。。心累。。
点赞 回复 分享
发布于 2017-09-26 22:25

相关推荐

04-29 22:35
门头沟学院 Java
牛友说改了名字能收到offer:旧图新发查看图片
点赞 评论 收藏
分享
评论
点赞
7
分享

创作者周榜

更多
牛客网
牛客企业服务