2021-09-06

题目
好久没练题了,一直忙许多乱七八糟的事情。最近要考甲级,所以重新刷甲级真题。这道题目不难,就是简单的查找与排序(说来说去,还是字符串的处理)。但是有个以前不知道,现在知道的点,关于C++中的endl与"\n"之间的区别。

在没有必要刷新输出流的时候应尽量使用cout<<"\n",过多的endl是影响程序执行效率底下的原因。
endl除了写’\n’进外,还调用flush函数,刷新缓冲区,把缓冲区里的数据写入文件或屏幕,考虑效率就用’\n’

因为大量使用endl,导致后面两个测试点一直运行超时。

#include <bits/stdc++.h>
using namespace std;
const int N=1e4+10;
struct student{
	string id;
	int score;
}stu[N];

struct node{
	string site;
	int peo;
}place[N];

bool cmp1(const student &a, const student &b){
	if(a.score!=b.score){
		return a.score > b.score;
	} else {
		return a.id < b.id;
	}
}

bool cmp2(const node &a, const node &b){
	if(a.peo!=b.peo){
		return a.peo > b.peo;
	} else {
		return a.site < b.site;
	}
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i=0; i<n; i++){
    	cin>>stu[i].id>>stu[i].score;
    }

    string t, info;
    for(int i=1; i<=m; i++){
    	cin>>t>>info;
    	cout<<"Case "<<i<<": "<<t<<" "<<info<<"\n";
    	if(t=="1"){
    		vector<student> v;
    		for(int j=0; j<n; j++){
    			if(stu[j].id.substr(0, 1)==info){
    				v.push_back(stu[j]);
    			}
    		}

    		if(v.size()){
    			sort(v.begin(), v.end(), cmp1);
    			for(int j=0; j<(int)v.size(); j++){
    				cout<<v[j].id<<" "<<v[j].score<<"\n";
    			}
    		} else {
    			printf("NA\n");
    		}
    	} else if(t=="2"){
    		int res=0, sum=0;
    		for(int j=0; j<n; j++){
    			if(stu[j].id.substr(1, 3)==info){
    				res+=1;
    				sum+=stu[j].score;
    			}
    		}

    		if(res){
    			printf("%d %d\n", res, sum);
    		} else {
    			printf("NA\n");
    		}
    	} else if(t=="3"){
    		unordered_map<string, int> mp;
    		for(int j=0; j<n; j++){
    			if(stu[j].id.substr(4, 6)==info){
    				mp[stu[j].id.substr(1, 3)]+=1;
    			}
    		}

    		if(mp.size()){
    			unordered_map<string, int>::iterator it;
    			int cnt=0;
    			for(it=mp.begin(); it!=mp.end(); it++){
    				place[cnt].site=it->first;
    				place[cnt].peo=it->second;
    				cnt+=1;
    			}

    			sort(place, place+cnt, cmp2);
    			for(int j=0; j<cnt; j++){
    				cout<<place[j].site<<" "<<place[j].peo<<"\n";
    			}
    		} else {
    			printf("NA\n");
    		}
    	} else {
    		printf("NA\n");
    	}
    }
	return 0;
}
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
最新发布
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值