oj---pat---a1028

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Student{
    char name[10];
    int id;
    int grade;
}student[100005];

bool cmp1(Student a,Student b){
    return a.id<b.id;
}
bool cmp2(Student a,Student b){
    int tmp=strcmp(a.name,b.name);
    if(tmp!=0){
        return tmp<0;
    }
    else return a.id<b.id;
}
bool cmp3(Student a,Student b){
    if(a.grade!=b.grade)
        return a.grade<b.grade;
    else return a.id<b.id;
}

int main(){
    //int cnt=0;
    int n,c;
    while(scanf("%d %d",&n,&c)!=EOF){
        if(n==0) break;
        for(int i=0;i<n;i++){
            scanf("%d %s %d",&student[i].id,student[i].name,&student[i].grade);
        }
        if(c==1) sort(student,student+n,cmp1);
        if(c==2) sort(student,student+n,cmp2);
        if(c==3) sort(student,student+n,cmp3);
        //printf("Case %d:\n",++cnt);
        for(int i=0;i<n;i++){
            printf("%06d %s %d\n",student[i].id,student[i].name,student[i].grade);
        }
    }
    return 0;
}
时间: 2024-08-07 10:38:22

oj---pat---a1028的相关文章

PAT 甲级 A1028 (2019/02/17)

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 100010; struct Student{ int id; char name[10]; int score; }stu[MAXN]; bool cmp1(Student a, Student b) { return a.id < b.id; //从小到大 } bool cmp

PAT甲级——A1028 List Sorting

Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, where N is the number of reco

PAT 1035

PAT的题有些很无聊的... (多数) 1 #include <vector> 2 #include <string> 3 #include <iostream> 4 #include <fstream> 5 6 using namespace std; 7 8 #define OJ 9 10 #ifdef OJ 11 #define fin cin 12 #endif 13 14 struct Student{ 15 string name; 16 boo

PAT 07-2 A+B和C

有两个值得注意的地方:1.变长数组(VLA)的使用,没想到PAT上的OJ竟然支持C99,一开始不知道就没用,看了看别人的,既然,还是用吧, 它有一点我不太喜欢,它不能像一般数组那样在声明时通过赋一个0让全部元素初始化为零,等等,有点理解了.2.long long长整型的格式化输入输出,都要在"%d"中间插入"ll",所以,算了吧,还是定义一个临时变量更方便.题设要求及代码实现如下 /* Name: Copyright: Author: Date: 01/04/15

PAT——甲级1046S:shortest Distance

这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input

PAT 1048数字加密

本题要求实现一种数字加密方法.首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10.Q 代表 11.K 代表 12:对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10.这里令个位为第 1 位. 输入格式: 输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔. 输出格式: 在一行中输出加密后的结果. 输入样例: 1234567 3

PAT 1009 说反话 C语言

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格. 输出格式:每个测试用例的输出占一行,输出倒序后的句子. 输入样例: Hello World Here I Come 输出样例: Come I Here World Hello 1 #include<stdio.h> 2 #

PAT 1006 换个格式输出 C语言

让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个"百".3个"十".以及个位的4. 输入格式:每个测试输入包含1个测试用例,给出正整数n(<1000). 输出格式:每个测试用例的输出占一行,用规定的格式输出n. 输入样例1: 234 输出样例1: BBSSS1

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个