题解 | 使用数组的方法做---#提取不重复的整数#
提取不重复的整数
https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1
#include <iostream> #include <vector> using namespace std; //计算10的n次幂 int caculateTen(int x, int vSize) { int mySum = 1; for (int i = 1; i <= (vSize - x - 1); i++) { mySum = mySum * 10; } return mySum; } int newNum(int n) { vector<int>v(9); int i = 0; for (; n % 10!= 0; i++) { v[i] = (n % 10); n = n /10; } v.erase(v.begin()+i,v.end()); //将多余的内容去除 for (int i = 0; i < v.size(); i++) { for (int j = 0; i > 0 && j < i; j++) { if (v[i] == v[j]) { v.erase(v.begin() + i); //将重复元素删掉 i = i - 1; //恢复i的位置 } } } int sum = 0; for (int i = 0; i < v.size(); i++) { sum = sum + v[i] * caculateTen(i, v.size()); } return sum; } int main() { int n; while (cin >> n) { // 注意 while 处理多个 case cout << newNum(n) << endl; } return 0; }