1055.The World's Richest
2015-01-21 19:34
309 查看
【题意】
给出一些人的名字、年龄、财富,求一定年龄范围内、一定人数内的财富榜
【思路】
先按财富、年龄、姓名顺序排个序,然后筛选出年龄符合要求的输出
【注意点】
需要进行剪枝才能过Case 1(目测Case 1的测试数据前面是1串同一年龄但不在要求范围内的人,若不剪枝会超时。其实我用先对年龄排序,再二分法找上下限位置的方法也能过Case 1,但Case 2过不去),跳过的人是排序后同一年龄里排在100名之后的,因为题目中说排行榜最多100个人
给出一些人的名字、年龄、财富,求一定年龄范围内、一定人数内的财富榜
【思路】
先按财富、年龄、姓名顺序排个序,然后筛选出年龄符合要求的输出
【注意点】
需要进行剪枝才能过Case 1(目测Case 1的测试数据前面是1串同一年龄但不在要求范围内的人,若不剪枝会超时。其实我用先对年龄排序,再二分法找上下限位置的方法也能过Case 1,但Case 2过不去),跳过的人是排序后同一年龄里排在100名之后的,因为题目中说排行榜最多100个人
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <cstdio> #include <cstring> using namespace std; typedef struct{ char name[9]; int age; int net_worth; }info; bool cmp(info i1, info i2){ if(i1.net_worth>i2.net_worth){ return 1; } else if(i1.net_worth==i2.net_worth && i1.age<i2.age){ return 1; } else if(i1.net_worth==i2.net_worth && i1.age==i2.age && strcmp(i1.name,i2.name)<0){ return 1; } else{ return 0; } } int main(int argc, char const *argv[]) { int n,k; vector<info> infos; vector<info>::iterator it; vector<int> filter; int ageCnt[200]; memset(ageCnt,0,sizeof(ageCnt)); scanf("%d%d", &n, &k); infos.resize(n); for(int i=0; i<n; i++){ scanf("%s%d%d", infos[i].name, &infos[i].age, &infos[i].net_worth); } sort(infos.begin(),infos.end(),cmp); int filterNum = 0; filter.resize(n); for(int i=0; i<n; i++){ if(++ageCnt[infos[i].age]<=100){ filter[filterNum++] = i; } } for(int i=0; i<k; i++){ int m,amin,amax,cnt; scanf("%d%d%d", &m, &amin, &amax); printf("Case #%d:\n", i+1); cnt = 0; for(int i=0; i<filterNum && cnt<m; i++){ int index = filter[i]; if(infos[index].age>=amin && infos[index].age<=amax){ printf("%s %d %d\n", infos[index].name, infos[index].age, infos[index].net_worth); cnt++; } } if(cnt==0){ printf("None\n"); } } system("pause"); return 0; }
相关文章推荐
- pat 1055. The World's Richest (25)
- PAT (Advanced) 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- pat 1055. The World's Richest (25)
- 1055. The World's Richest
- pat 1055. The World's Richest (25)
- PAT 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 浙江大学PAT_甲级_1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- PAT A 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 【C++】PAT(advanced level)1055. The World's Richest (25)*
- 1055. The World's Richest (25)
- ZJU-PAT 1055. The World's Richest (25) 浙大2013年上机复试第二题
- PAT (Advanced Level) 1055. The World's Richest (25) 年龄区间内最有钱的人,结构体排序