1.
给出一个字符串,将重复的字符去除,仅保留第一次出现的字符,且保持去重后的字符在原字符串中的顺序不变。
输入数据是一个字符串(不包含空格)
输出去重后的字符串
输入:12ere2
输出:12er
#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;
int main()
{
string instr,outstr;
unordered_set<char> sc;
getline(cin,instr);
for(auto c:instr)
{
if(sc.find(c)==sc.end())
{
sc.insert(c);
outstr.append(1,c);
}
}
cout<<outstr<<endl;
return 0;
}
2
代码如下:
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int main()
{
vector<pair<int,int>> time{{12,13},{13,14},{14,15},{15,16},
{16,17},{17,18},{18,19},{19,20}};
vector<pair<int,int>> people;
map<pair<int,int>,int> result;
int a,b;
while(true)
{
cin>>a;
if(cin.get()==','){
cin>>b;
if(a==-1&&b==-1){
break;}
people.push_back(make_pair(a,b));
}
}
for(pair<int,int> pa:people)
{
for(pair<int,int> t:time)
{
if(pa.first<=t.first&&pa.second>=t.second)
{
++result[t];
}
}
}
for(pair<pair<int,int>,int> re:result)
{
cout<<'['<<re.first.first<<','<<re.first.second<<')'<<':'<<re.second<<endl;
}
return 0;
}
3.
代码如下:
#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<string>
using namespace std;
int main()
{
stack<int> num;
stack<char> ops;
string str;
getline(cin,str);
int result;
int i=0;
for(;i<str.size();)
{
int t=0;
while(str[i]==' ') {
++i;
++t;
}
if(t>1){ //' ' is more
result=-1;
break;}
if(str[i]!='.'&&str[i]<'0'||str[i]>'9') {
if(str[i]==')'){
char op=ops.top();
ops.pop();
if(op=='^'){
int re=num.top();
num.pop();
num.push(re+1);
}else if(op=='+'||op=='*'){
int a=num.top();
num.pop();
int b=num.top();
num.pop();
if(op=='+'){
num.push(a+b);
}else{
num.push(a*b);}
}else // other op{
result=-1;
break;
}
ops.pop(); //delete'('
if(ops.empty()){ //the tree is end
result=num.top();
break;
}
}else{
ops.push(str[i]);
}
++i;
}else if(str[i]!='.'){
t=i;
while(str[i]>='0'&&str[i]<='9'){
++i;
}
num.push(stoi(str.substr(t,i-t)));
}else{ //'.'
break;
result=-1;
}
}
if(i==str.size()){
result=-1;
}
cout<<result<<endl;
}