OJ9
提取不重复的整数
输入int型的整数————9876673
从右往左输出,不重复的新整数————37689
//方法一:将原int型整数以string类型输入,用一个vector对重复的字符进行操作,最后输出 #include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ string s; cin >> s; vector<char> res; for(int i = s.size()-1; i >= 0; i--){ if(find(res.begin(),res.end(),s[i]) != res.end()){ continue; } res.push_back(s[i]); cout << s[i]; } } //方法二:用类似hash表的概念,用数组判定重复出现的数字,如果重复出现直接pass,/10进入下一数字 //同时加上其实条件的判定之后更加完善 #include<iostream> using namespace std; int main() { int n; int a[10]={0}; int num=0; cin>>n ; while(n) { if(a[n%10]==0) { if(num == 0 && n % 10 == 0){ a[n%10]=0; } else{ a[n%10]++;//这一步是更新,遇到下次相同的数会跳过 num=num*10+n%10; } } n/=10; } cout<<num<<endl; return 0; } // 方法三:还是输入整数,但是转换成string类型进行处理,最后输出整型,防止有些判题,必须是整数输入输出 #include<bits/stdc++.h> using namespace std; int main() { int nums; cin >> nums; string str = to_string(nums); unordered_set<char> set; reverse(str.begin(), str.end()); string res; for(char c : str) if(set.count(c) != 1) { res += c; set.insert(c); } cout << stoi(res) << endl; return 0; }