京东笔试

京东笔试编程题,姓名排序那一题,有人用C+实现出来的吗,或者有啥思路吗#京东##笔试题目#
全部评论
#include<bits/stdc++.h> typedef long long ll; const int maxn = 1e4 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; using namespace std; struct node {     string x, m; }f[maxn]; map<string, int>mp; bool cmp(node a, node b) {     return mp[a.x] > mp[b.x]; } int main() {     int i = 0;     while (cin >> f[i].x >> f[i].m) {         mp[f[i].x]++;         i++;     }     stable_sort(f, f + i, cmp);     for (int x = 0; x < i; x++) {         cout << f[x].x << " " << f[x].m << endl;     }     return 0; }
点赞 回复 分享
发布于 2019-08-24 21:48
用Python做的,A了91, 思路是 每位学生拥有一个号码牌,上面写着【姓出现次数】* N + 录入列表倒序之后的索引 然后按照号码牌的数字倒序排列即符合题意
点赞 回复 分享
发布于 2019-08-27 11:47
菜鸡的并不简洁的代码,自定义了比较函数,利用map结构保存姓出现的次数,便于比较时候查找。 之前用二维vector存储,过了91%,改了一维以后来不及提交了,不知道能过多少........ #include <iostream> #include <map> #include <vector> using namespace std; map<string,int> obj; vector<string> in; bool compare(string a,string b){ string it1,it2; int n1=a.find_first_of(" "); int n2=b.find_first_of(" "); it1.append(a,0,n1); it2.append(b,0,n2); if(obj.find(it1)->second<obj.find(it2)->second) return true; else return false; } void sort_xm(){ int i=0,j=0; string tmp1; for(i=0;i<in.size();i++){ for(j=0;j<in.size()-i-1;j++){ if(compare(in[j],in[j+1])){ tmp1=in[j]; in[j]=in[j+1]; in[j+1]=tmp1; } } } } int main(){ map<string,int>::iterator it; string x,m; int i=0; while(cin>>x>>m){ it=obj.find(x); if(it!=obj.end()) it->second+=1; else obj.insert(pair<string,int>(x,1)); string tmp=""; tmp+=x+" "+m; in.push_back(tmp); } sort_xm(); for(i=0;i<in.size();i++) cout<<in[i]<<endl; return 0; }
点赞 回复 分享
发布于 2019-08-27 11:17
c++ 第一题 #include<iostream> #include<vector> #include<algorithm> using namespace std; int N; int main() {     while(cin >> N) {         vector<int> arr;         for (int i = 0; i < N; i++) {             int a;             cin >> a;             arr.push_back(a);         }         vector<int> dp(arr.begin(), arr.end());         sort(arr.begin(), arr.end());         int num = 0;         int xmin;         int xmax;         for (int i = 0; i < N; i++) {             if (dp[i] == arr[i])                 num += 1;             else             {                 xmin = dp[i];                 xmax = dp[i];                 int left = i;                 for (int j = i; j < N; j++) {                     xmin = min(xmin, dp[j]);                     xmax = max(xmax, dp[j]);                     if (xmin == arr[left] && xmax == arr[j]) {                         num++;                         i = j;                         break;                     }                 }             }         }         cout << num;     }     return 0; }
点赞 回复 分享
发布于 2019-08-25 15:53
#include <iostream> #include <vector> #include <queue> #include <map> #include <algorithm> using namespace std; struct node{     string first;     string second;     int count; }; bool cmp(node a, node b) {     return a.count>b.count; } int main() {     freopen("in.txt","r",stdin);     string first;     string second;     vector<node> data;     while(cin>>first>>second)     {         node node1;         node1.first = first;         node1.second = second;         node1.count = 0;         data.push_back(node1);     }     map<string, int> mymap;     //第一遍遍历 统计频率     for(int i=0; i<data.size(); i++)         mymap[data[i].first]++;     for(int i=0; i<data.size(); i++)         data[i].count = mymap[data[i].first];     sort(data.begin(), data.end(), cmp);     for(int i=0; i<data.size(); i++)         cout<<data[i].first<<" "<<data[i].second<<endl; } 不知道能过多少。。。。
点赞 回复 分享
发布于 2019-08-24 21:41
我花几分钟写了个我觉得一定错的 结果a了百分之63的用例 我人都懵了😂
点赞 回复 分享
发布于 2019-08-24 21:30
按优先级分组排序
点赞 回复 分享
发布于 2019-08-24 21:29

相关推荐

牛客928043833号:在他心里你已经是他的员工了
点赞 评论 收藏
分享
评论
点赞
5
分享

创作者周榜

更多
牛客网
牛客企业服务