输入第一行包括1个整数N,3<=N<=30,代表输入数据的个数。 接下来的N行有N个职工的信息: 包括职工号(整数), 姓名(字符串,长度不超过10), 年龄(1<=age<=100)。
可能有多组测试数据,对于每组数据, 输出结果行数为N和3的较小值,分别为年龄最小的职工的信息。 关键字顺序:年龄>工号>姓名,从小到大。
5 501 Jack 6 102 Nathon 100 599 Lily 79 923 Lucy 15 814 Mickle 65
501 Jack 6 923 Lucy 15 814 Mickle 65
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; typedef struct Name{ int age; int ID; string name; }Name; static bool comp(Name a,Name b) { if(a.age<b.age) return true; else if(a.age==b.age) { if(a.ID<b.ID) return true; else if(a.ID==b.ID) { if(a.name<b.name) return true; } } return false; } int main() { int N; while(cin>>N) { vector<Name> vec; Name temp; for(int i=0;i<N;i++) { cin>>temp.ID>>temp.name>>temp.age; vec.push_back(temp); } sort(vec.begin(),vec.end(),comp); for(int i=0;i<3;i++) { cout<<vec[i].ID<<" "<<vec[i].name<<" "<<vec[i].age<<endl; } } return 0; }
//就是比较,然后sort函数使用的时候不要忘记了要加上比较的函数 //输出的时候考虑到小于3的情况所以条件为(i<3&&i<n)避免越界错误 #include<bits/stdc++.h> using namespace std; struct Staff{ int age; int id; char name[30]; }; bool cmp(Staff a,Staff b){ if(a.age<b.age) return 1; else if(a.age==b.age){ if(a.id<b.id) return 1; else if(a.id==b.id){ int len1=strlen(a.name),len2=strlen(b.name); for(int i=0,j=0;i<len1&&j<len2;i++,j++) if(a.name[i]>b.name[j]) return 0; return 1; } else return 0; } else return 0; } int main(){ int n; while(cin>>n){ Staff *staff=new Staff[n]; for(int i=0;i<n;i++) cin>>staff[i].id>>staff[i].name>>staff[i].age; sort(staff,staff+n,cmp); for(int i=0;i<3&&i<n;i++) cout<<staff[i].id<<" "<<staff[i].name<<" "<<staff[i].age<<endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <cstring> using namespace std; //自定义比较排名问题,这里用vector比较好 typedef struct worker{ int id; int age; string name; }Worker; bool cmp(Worker a,Worker b){ if(a.age!=b.age){ return a.age<b.age; } else if(a.id!=b.id){ return a.id<b.id; } return a.name<b.name; } int main(){ int n; cin>>n; Worker temp; vector<Worker> works; for(int i = 0;i < n;i++){ cin>>temp.id>>temp.name>>temp.age; works.push_back(temp); } sort(works.begin(),works.end(),cmp); if(n > 3) n = 3; for(int i = 0;i < n;i++){ cout<<works[i].id<<" "<<works[i].name<<" "<<works[i].age<<endl; } return 0; }
class Info implements Comparable<Info>{ public int age; public int number; public String name; public Info(int number, String name, int age){ this.age = age; this.number = number; this.name = name; } @Override public int compareTo(Info o){ if(this.age!= o.age){ return this.age-o.age; }else{ return this.number-o.number; } } }
# include <stdio.h> # include <string.h> int main() { struct Person { int number; char name[11]; int age; }; int n; while(scanf("%d", &n) != EOF) { int i,j,k; Person a[30]; for (i = 0; i < n; i++) scanf("%d %s %d", &a[i].number, a[i].name, &a[i].age); k = (3>n)? n : 3; for (i = 0; i < k; i++) for (j = 1 + i; j < n; j++) if (a[i].age>a[j].age || (a[i].age == a[j].age && a[i].number>a[j].number)|| (a[i].age == a[j].age && a[i].number == a[j].number&& strcmp(a[i].name, a[j].name)>0)) { Person temp = a[j]; a[j] = a[i]; a[i] = temp; } for (i = 0; i < k; i++) printf("%d %s %d\n", a[i].number, a[i].name, a[i].age); } return 0; }
#include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; typedef struct staff { int num;//工号 char name[10]; int age; }; bool operator<(const staff & s_1,const staff & s_2) { if(s_1.age == s_2.age) { if(s_1.num == s_2.num) return strcmp(s_1.name, s_2.name); else return s_1.num < s_2.num; } else return s_1.age < s_2.age; } int main(void) { int n; vector<staff> s; while(cin >> n) { s.clear(); for(int i = 0;i < n;i++) { staff temp; cin >> temp.num >> temp.name >> temp.age; s.push_back(temp); } sort(s.begin(),s.end()); for(int i = 0;i < 3;i++) { cout << s[i].num << ' ' << s[i].name << ' ' << s[i].age; cout << endl; } } return 0; }
#include<stdio.h> #include<string.h> struct Worker{ int num; char name[10]; int age; }wo[30]; int main() { int n,i,j,tnum,tage;char tname[10]; scanf("%d",&n);//输入 for(i=0;i<n;i++) scanf("%d%s%d",&wo[i].num,wo[i].name,&wo[i].age); for(i=0;i<n-1;i++)//先按照年龄排序 for(j=0;j<n-1-i;j++) if(wo[j].age>wo[j+1].age) {//全部交换 tnum=wo[j].num;strcpy(tname,wo[j].name);tage=wo[j].age; wo[j].num=wo[j+1].num;strcpy(wo[j].name,wo[j+1].name);wo[j].age=wo[j+1].age; wo[j+1].num=tnum;strcpy(wo[j+1].name,tname);wo[j+1].age=tage; } for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(wo[j].age==wo[j+1].age)//年龄相等的时候看工号 { if(wo[j].num>wo[j+1].num)//对工号排序 {//全部交换 tnum=wo[j].num;strcpy(tname,wo[j].name);tage=wo[j].age; wo[j].num=wo[j+1].num;strcpy(wo[j].name,wo[j+1].name);wo[j].age=wo[j+1].age; wo[j+1].num=tnum;strcpy(wo[j+1].name,tname);wo[j+1].age=tage; } if(wo[j].num==wo[j+1].num)//年龄工号全部相等看名字 { if(strcmp(wo[j].name,wo[j+1].name)>0)//对姓名排序 {//全部交换 tnum=wo[j].num;strcpy(tname,wo[j].name);tage=wo[j].age; wo[j].num=wo[j+1].num;strcpy(wo[j].name,wo[j+1].name);wo[j].age=wo[j+1].age; wo[j+1].num=tnum;strcpy(wo[j+1].name,tname);wo[j+1].age=tage; } } } for(i=0;i<3;i++) printf("%d %s %d\n",wo[i].num,wo[i].name,wo[i].age); }
/** \brief vector容器可以用sort()函数,只要自定义一下排序规则即可,sortFun()函数 * * \param * \param * \return * */ #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; struct info{ unsigned id; string name; unsigned age; }; bool sortFun(const info &a,const info &b){ return a.age<b.age||(a.age==b.age&&a.id<b.id)||((a.age==b.age&&a.id==b.id)&&a.name<b.name); //按规则写好返回值 } int main() { int N,print_num; vector<info> all_info; info per; cin>>N; (N>3)?(print_num=3):(print_num=N); for(int i=0;i<N;++i){ cin>>per.id>>per.name>>per.age; all_info.push_back(per); } sort(all_info.begin(),all_info.end(),sortFun); for(int i=0;i<print_num;++i) cout<<all_info[i].id<<' '<<all_info[i].name<<' '<<all_info[i].age<<endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef struct{ int id; string name; int age; }employee; bool cmp(employee epy1, employee epy2) { if(epy1.age == epy2.age && epy1.id == epy2.id) return epy1.name < epy2.name; else if(epy1.age == epy2.age && epy1.id != epy2.id) return epy1.id < epy2.id; else return epy1.age < epy2.age; } int main() { int n; cin >> n; employee *epy = new employee[n]; for (int i = 0; i < n; i++) { cin >> epy[i].id >> epy[i].name >> epy[i].age; } sort(epy, epy + n, cmp); for (int i = 0; i < (n < 3 ? n : 3); i++) { cout << epy[i].id << " " << epy[i].name << " " << epy[i].age << endl; } delete []epy; }
#include<bits/stdc++.h>
using namespace std;
struct staff{
int number;
char name[11];
int age;
}st[30];
bool cmp(staff a,staff b){
if(a.age!=b.age)return a.age<b.age;
else if(a.number!=b.number)return a.number<b.number;
else return strcmp(a.name,b.name)<0;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)
scanf("%d %s %d",&st[i].number,st[i].name,&st[i].age);
sort(st,st+n,cmp);
for(int i=0;i<n;i++){
if(i==3)break;
printf("%d %s %d\n",st[i].number,st[i].name,st[i].age);
}
}
}
//标准答案 #include <iostream> #include <string.h> #include <vector> #include <algorithm> using namespace std; struct staff{ int id; char name[11]; int age; }; bool cmp(const staff s1,const staff s2); bool cmp(const staff s1,const staff s2){ if(s1.age==s2.age) return s1.id<s2.id; if(s1.age==s2.age&&s1.id==s2.id) return strcmp(s1.name,s2.name)<0; return s1.age<s2.age; } int main(){ int n; while(cin>>n){ vector<staff> staffs(n); int i; for(i=0;i<n;i++) cin>>staffs[i].id>>staffs[i].name>>staffs[i].age; stable_sort(staffs.begin(),staffs.end(),cmp); int min=n>3?3:n; for(i=0;i<min;i++) cout<<staffs[i].id<<" "<<staffs[i].name<<" "<<staffs[i].age<<endl; } return 0; }
#include<stdio.h> #include<string> #include<vector> #include<algorithm> using namespace std; typedef struct Person{ int id; string name; int age; }Person; bool compare(Person a,Person b){ if(a.age!=b.age){ return a.age<b.age; }else{ if(a.id!=b.id){ return a.id<b.id; }else{ return a.name<b.name; } } } int main(){ int N; scanf("%d",&N); vector<Person> vec; int id,age; char name[100]={0}; for(int i=0;i<N;i++){ scanf("%d %s %d",&id,name,&age); Person p; p.id = id; p.name = name; p.age = age; vec.push_back(p); } sort(vec.begin(),vec.end(),compare); for(int i=0;i<3;i++){ printf("%d %s %d\n",vec[i].id,vec[i].name.c_str(),vec[i].age); } }