浙大PAT甲级 1055
2016-08-23 20:24
411 查看
简单题,首先将输入的信息先后按照worth递减,年龄递增,名字字典序递增来进行排序。
然后对于每个查询,从头开始遍历输出最多M个的年龄在范围内的信息。
AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#define inf 26*26*26*10+5
using namespace std;
struct node
{
char name[10];
int age;
int data;
};
bool cmp(node x,node y)
{
if(x.data!=y.data)
{
return x.data>y.data;
}
else
{
if(x.age!=y.age)
return x.age<y.age;
else
return strcmp(x.name,y.name)<0;
}
}
vector<node> v;
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
node tmp;
scanf("%s %d %d",tmp.name,&tmp.age,&tmp.data);
v.push_back(tmp);
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<m;i++)
{
int d,l,r;
scanf("%d %d %d",&d,&l,&r);
int num=0;
printf("Case #%d:\n",i+1);
int len=v.size();
for(int i=0;i<len;i++)
{
if(v[i].age>=l&&v[i].age<=r&&num<d)
{
printf("%s %d %d\n",v[i].name,v[i].age,v[i].data);
num++;
}
}
if(num==0)
{
printf("None\n");
}
}
}
然后对于每个查询,从头开始遍历输出最多M个的年龄在范围内的信息。
AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#define inf 26*26*26*10+5
using namespace std;
struct node
{
char name[10];
int age;
int data;
};
bool cmp(node x,node y)
{
if(x.data!=y.data)
{
return x.data>y.data;
}
else
{
if(x.age!=y.age)
return x.age<y.age;
else
return strcmp(x.name,y.name)<0;
}
}
vector<node> v;
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
{
node tmp;
scanf("%s %d %d",tmp.name,&tmp.age,&tmp.data);
v.push_back(tmp);
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<m;i++)
{
int d,l,r;
scanf("%d %d %d",&d,&l,&r);
int num=0;
printf("Case #%d:\n",i+1);
int len=v.size();
for(int i=0;i<len;i++)
{
if(v[i].age>=l&&v[i].age<=r&&num<d)
{
printf("%s %d %d\n",v[i].name,v[i].age,v[i].data);
num++;
}
}
if(num==0)
{
printf("None\n");
}
}
}
相关文章推荐
- Lightoj1045 Digits of Factorial(数论)
- Lightoj 1138 ( Trailing Zeroes (III))
- 美团笔试题(JavaScript)
- CodeForces 598A Tricky Sum
- 物联网框架SuperIO 2.2.9和ServerSuperIO 2.1同时更新,更适用于类似西门子s7-200发送多次数据,才能读取数据的情况
- java内存结构划分与java虚拟机调优
- React Native几个重要的属性
- hibernate-sql
- asp.net mvc 简易通用自定义Pager实现分页
- JavaEE完整技术体系文章陆续上映!!!
- 旅行(最短路)
- logback.xml常用配置详解<configuration> and <logger>
- C# 文件夹,文件操作
- UDP广播问题
- 图的深度遍历
- 求乘方取模(快速幂+慢速乘法模板)
- Java静态分派与动态分派
- 线性同余方程及例题 codeforces 710D
- 4000 java.lang.UnsatisfiedLinkError: com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoa
- Android学习笔记--3.Intent的使用