目录
第一题-组队竞赛
思路:
1.首先将所有数据存放到一个数组当中
2.将数组排序
3.找到arr.len-2*(i+1)下标位置为中间数据,然后相加,i从0开始
比如现在一个数组 1 2 3 4 5 6,那么取中间数据相加就是3+5
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
//建立数组a
int n;
while(cin>>n)
{
long long sum = 0;
vector<int>a;
a.resize(n*3);
//将数据放到数组a中
for(int i = 0; i < 3*n; ++i)
{
cin>>a[i];
}
//排序
sort(a.begin(),a.end());
//求和
for(int i = 0; i < n; ++i)
{
sum += a[a.size()-2*(i+1)];
}
cout <<sum<< endl;
}
}
第二题-删除公共字符
思路:
1.将两行字符记录在str1和str2当中
2.将str2当中的字符作为下标记录在hash中
3.将str1当中所有字符作为下标依次与hash当中比较,然后放到ret当中,返回ret
#include<iostream>
#include<string>
using namespace std;
int main ()
{
//获取行字符串不用cin
string str1;
string str2;
getline(cin,str1);
getline(cin,str2);
//用哈希记录所有字符串,将要排除的字符串出现记录在hash中
int hash[256] = {0};
for(int i = 0; i < str2.size(); ++i)
{
hash[str2[i]]++;
}
//如果str1字符依次与hash比较如果没有出现则将str1中字符放到ret中
string ret = "";
for(int i = 0; i < str1.size(); ++i)
{
if(hash[str1[i]] == 0)
{
ret += str1[i];
}
}
cout << ret <<endl;
}