PAT1028. List Sorting (25)---strcmp

題目連接爲:https://www.patest.cn/contests/pat-a-practise/1028c++

1028. List Sorting (25)

Excel can sort records according to any column. Now you are supposed to imitate this function.git

Input數組

Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).ide

Output函數

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.測試

Sample Input 1
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

題目其實特別簡單,應該是考察sort的使用。this

<1>因爲時間的限制,輸入輸出最好是scanf 和 printf,不然最後一個測試點將過不去;spa

<2>一樣由於時間的限制,修改vector爲數組(不過應該差異不會太大,主要仍是<1>)。3d

論基礎知識紮實的重要性,strcmp的結果是返回正數或者負數,而不是返回-1和1。這個點主要用在cmp函數中。code

設這兩個字符串爲str1,str2,strcmp(str1,str2)的含義是:若str1=str2,則返回零;若str1<str2,則返回負數;若str1>str2,則返回正數。
 
如下是代碼
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define maxn 100005
 4 #define For(I,A,B) for(int I = (A); I < (B); I++)
 5 
 6 class student
 7 {
 8     public:
 9     char id[15],name[15];
10     int grade;
11 };
12 student stu[maxn];
13 int n,c;
14 bool cmp(const student &a,const student &b)
15 {
16     if(c == 1)
17     {
18         if(strcmp(a.id,b.id))
19             return strcmp(a.id,b.id) < 0;
20     }
21     else if(c == 2)
22     {
23         if(strcmp(a.name,b.name))
24             return strcmp(a.name,b.name) < 0;
25         return strcmp(a.id,b.id) < 0;
26     }
27     else if(c == 3)
28     {
29         if(a.grade != b.grade)
30             return a.grade < b.grade;
31         return strcmp(a.id,b.id) < 0;
32     }
33 }
34 
35 int main()
36 {
37     freopen("1028.in","r",stdin);
38     while(scanf("%d%d",&n,&c) != EOF)
39     {
40         //stu.clear();
41         For(i,0,n)
42         {
43             scanf("%s%s%d",&stu[i].id,&stu[i].name,&stu[i].grade);//>>stu[i].id>>stu[i].name>>stu[i].grade;
44         }
45         sort(stu,stu +n,cmp);
46         For(i,0,n)
47         {
48             printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);//cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
49         }
50     }
51     return 0;
52 }
PAT1028
相關文章
相關標籤/搜索