输入:
16#7B
10#123
4#1323
8#16
END
输出:
8#16
#号之前是进制数(2~16),之后为该进制数下的数值,找出输入当中不同与其他数的数值。下例当中前三个表示123,最后一个表示14,所以输出8#16。代码的编译环境为vs2017
#include <iostream>
#include <string>
#include<vector>
#include<cstdlib>
#include<algorithm>
using namespace std;
int transform(string& s){
int data1 = atoi(s.c_str());//data1为转换基数
size_t begin = 0;
while(s[begin] != '#'){
++begin;
}
++begin;
int integer = 0;
integer = stoi(s.substr(begin), &begin, data1);
return integer;
}
int find_diff(vector<int>& res){
int num = res[0];
int times = count(res.begin(), res.end(), num);
if(times == 1)
return 0;
else{
for(int i = 1; i < res.size(); ++i){
if(res[i] != num)
return i;
}
}
}
int main(){
string s;
vector<string> svec;
vector<int> res;//存放转换完之后的十进制数
while(cin >> s && s != "END"){
svec.push_back(s);
int dec = transform(s);
res.push_back(dec);
}
int i = find_diff(res);//找只出现一次的数的下标
for (auto i : res)
cout<<i<<" ";
cout << endl;
cout<<svec[i];
system("pause");
return 0;
}