题解 | #提取不重复的整数#

提取不重复的整数

https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1

#include <iostream>
#include <string>
using namespace std;

int main() {
    string strTest;
    cin >> strTest;
    int length = strTest.size();
    if(stoi(strTest.substr(length - 1, 1)) == 0){  //如果输入的最后一位是0,则剔除。
        strTest.erase(length - 1, 1);
        length--;
    }
    int arr[10] = {0};  //数字不超过9个数。使用数组下标存储出现过的数字并赋值排序
    for(int i = 0; i < length; i++){
        if(arr[stoi(strTest.substr(length - 1 - i, 1))] == 0){  //对应数字是否出现
            arr[stoi(strTest.substr(length - 1 - i, 1))] = i + 1;  //对应数字第几次出现
        }
    }
    for(int i = 1; i < 9; i++){  //数据范围是10^8以下,所以位数<9即可
        for(int j = 0; j < 10; j++){  //对数组的值进行从小到大遍历并取下标。
            if(arr[j] == i){
                cout << j;
            }
        }
    }
    cout << endl;
}




全部评论

相关推荐

我面试,她问我有女朋友没
不甜主义:考验稳定性,单身牛马更注意力集中
点赞 评论 收藏
分享
牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务