PAT甲级 1028 List Sorting (25分)(cin cout 超时问题)

注意: 用scanf 和printf 进行输入输出 否则超时

cin,cout速度慢的原因就是它会将数据先读入缓冲区,然后再读入,所以与scanf的直接读入会有点时间差距。

1.换成scanf 和 printf输入输出

2.加一条语句

ios::sync_with_stdio(false);

题目代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 0x3f3f3f3f;
 7 typedef long long ll;
 8 struct st{
 9     char num[10],name[10];
10     int grade;
11 }s[100005];
12 bool cmp1(st p,st q) {
13     return (strcmp(p.num,q.num) < 0);
14 }
15 bool cmp2(st p,st q) {
16     if(strcmp(p.name,q.name) != 0) return (strcmp(p.name,q.name) < 0);
17     return (strcmp(p.num,q.num) < 0);
18 }
19 bool cmp3(st p,st q) {
20     if(p.grade != q.grade) return p.grade < q.grade;
21     return (strcmp(p.num,q.num) < 0);
22 }
23 int main() {
24     int n,c;
25     scanf("%d%d",&n,&c);
26     for(int i = 0; i < n; i++) {
27         scanf("%s%s%d",s[i].num,s[i].name,&s[i].grade);
28     }
29     if(c == 1) sort(s,s+n,cmp1);
30     else if(c == 2) sort(s,s+n,cmp2);
31     else if(c == 3) sort(s,s+n,cmp3);
32     for(int i = 0; i < n; i++) {
33         printf("%s %s %d\n",s[i].num,s[i].name,s[i].grade);
34     }
35     return  0;
36 }

原文地址:https://www.cnblogs.com/LLLAIH/p/12258090.html

时间: 2024-10-08 08:01:32

PAT甲级 1028 List Sorting (25分)(cin cout 超时问题)的相关文章

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   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, wh

PAT Advanced 1028 List Sorting (25分)

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

1028 List Sorting (25 分)

1028 List Sorting (25 分) 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, wher

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names