《面試準備》中興2018筆試題

 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string.h>

using namespace std;

int output;
struct salary{
    int money;
    int cnt;
};
bool findx(const salary &s){
    return s.money == output;
}
bool cmp(const salary &s1,const salary &s2){
    if(s1.cnt==s2.cnt)
        return s1.money>s2.money;
    else{
        return s1.cnt>s2.cnt;
    }
}
int main()
{
    vector<salary>sal;
    salary mysal;
    stringstream ss;
    string s = "200,300,100,200,100,300,100,200,300,100,200,100,300";
    string num;
    ss<<s;
    while(getline(ss,num,','))
	{
	    stringstream sss;
	    sss<<num;
	    sss>>output;
	    auto f = find_if(sal.begin(),sal.end(),findx);
	    if(f!=sal.end())
            f->cnt++;
        else{
            mysal.cnt = 1;
            mysal.money = output;
            sal.push_back(mysal);
        }
	}
    sort(sal.begin(),sal.end(),cmp);
	for(auto it=sal.begin();it!=sal.end();it++){
        cout<<it->money<<" : "<<it->cnt<<endl;
	}
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章