Team competition, delete public characters solution

Table of contents

1. Team competition

2. Delete common characters 


1. Team competition

Team competition__nowcoder.com

answer:

Key point: The level value of the team is equal to the second highest level value of the player

Details: player level value <= 10^9, int type data range < 2147483647, three 10^9 have exceeded the int data range, so we use long long type to calculate the combined value.

        To maximize the combined value, we need to obtain the largest second level value each time, then we should choose 2 large numbers and 1 decimal number for our division, and ensure that the second level value is as large as possible.

        It's easy to think about it this way, we only need to sort the data from small to large, and take the second largest number backwards .

 Assuming there are n groups, search for n times, and the number of data is size. In the array, there is a formula for finding the second largest number: a[size-2*(i+1)]

 code:

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
    int n=0;
    while(cin>>n)
    {
        long long sum=0;
        vector<int>v;
        v.resize(3*n);
        for(int i=0;i<3*n;i++)
        {
            cin>>v[i];
        }
        //排序
        sort(v.begin(),v.end());
        for(int i=0;i<n;i++)
        {
            //公式:每次找划分第二大的数,划分最小数+2最大数,sum+第二大数
            sum+=v[v.size()-2*(i+1)];
        }
        cout<<sum<<endl;
    }
    return 0;
}

2. Delete common characters 

Delete common characters__Nioke.com (nowcoder.com)

answer:

Idea: To delete common characters, we only need to mark the common characters. When traversing the first string, if the character has been marked, it will be deleted. We can use hash mapping (unordered_map is also OK), here we Use an array of 256 (ASCII codes have 256 characters in total) int size for mapping statistics.

code:

#include <iostream>
#include<string>
using namespace std;

int main() {
    string str1;
    string str2;
    getline(cin,str1);
    getline(cin,str2);
    //采用哈希映射的思想
    int hash[256]={0};
    for(int i=0;i<str2.size();i++)
    {
        hash[str2[i]]++;
    }
    string res="";
    for(int i=0;i<str1.size();i++)
    {
        if(!hash[str1[i]])
            res+=str1[i];
    }
    cout<<res<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/bang___bang_/article/details/131765863