赞
踩
#include <bits/stdc++.h>
using namespace std;
int arr[26];
int main()
{
string str;
cin >> str;
vector<int> vec;
for(int i=0; i<str.size(); i++){
int temp = str[i]-'a';
arr[temp]++;
}
int minNum = 0;
//找出最少的字符 一定要排除字符数量为0的情况
for(int i=0; i<26; i++){
if(arr[i] != 0){
minNum = i;
break;
}
}
for(int i=0; i<26; i++){
if(arr[i] < arr[minNum] && arr[i] != 0)
minNum = i;
}
//将数量最少的字符对应的数字全部加入vec
for(int i=0; i<26; i++){
if(arr[i] == arr[minNum])
vec.push_back(i);
}
for(int i=0; i<str.size(); i++){
bool flag = true;
int temp = str[i]-'a';
for(int j=0; j<vec.size(); j++){//判断字符是否主要删除
if(vec[j] == temp){
flag = false;
break;
}
}
if(flag)
cout << str[i];
}
return 0;
}

剑指offer原题 牛客网题解 点击链接
#include <iostream>
#include <vector>
#define min(a, b) (a<b ? a : b);
using namespace std;
int getUglyNumber(int index) {
if(index < 7)
return index;
vector<int> res(index);
res[0] = 1;
int t2 = 0, t3 = 0, t5 = 0, i;
for(i = 1; i < index; ++i){
int minTemp = min(res[t2]*2, res[t3]*3);
res[i] = min(res[t5]*5, minTemp);
if(res[i] == res[t2]*2)
t2++;
if(res[i] == res[t3]*3)
t3++;
if(res[i] == res[t5]*5)
t5++;
}
return res[index - 1];
}
int main()
{
int n;
cin >> n;
cout << getUglyNumber(n) << endl;
return 0;
}

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。