Review pat1055 compare cin with scanf,etc..

Now that we prefer <iostream> to <stdio.h> ,why we use <stdio> in some acm questions?everyone knows the reason.well,i am not willing to do the same thing.and not only I think that way.Indeed,after searching online,I found there are three ways to improve the perfomance for <iostream>.

1.At the first line in main function,add :std::ios_base::sync_with_stdio(false).which cancel theSynchronization between <iostream> and <cstdio>;

2.At the second line in main function,add: std::cin.tie(0).which leads to that cin ties nothing.cin ties cout at first.

3.For all endl, use '\n' or"\n" instead.

Now let us get back pat1055,which we can change in three ways I mentiond above.and we get correct answer.

ps:cin>>string and cin>>char* are almost the same.if we do not filter the name that supass 100,the second case still cannot let us pass.


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

struct Person
{
	char name[9];
	int age;
	int worth;
};
struct Comp
{
	bool operator()(Person const &per_a,Person const &per_b)
	{
		if(per_a.worth==per_b.worth)
		{
			if(per_a.age==per_b.age)
			{
				return strcmp(per_a.name,per_b.name)<0;
			}
			return per_a.age<per_b.age;
		}
		return per_a.worth>per_b.worth;
	}
};

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m;
	cin>>n>>m;
	Person arr[n];
	for(int i=0;i<n;++i)
	{
		cin>>arr[i].name>>arr[i].age>>arr[i].worth;
	}
	sort(arr,arr+n,Comp());
	bool mark[n];
	memset(mark,0,sizeof(mark));
	int cnt[201]={0};
	int b[n];
	int j=0;
	for(int i=0;i<n;++i)
	{
		if(cnt[arr[i].age]<100)
		{
			b[j++]=i;
			++cnt[arr[i].age];
		}
	}
	for(int i=0;i<m;++i)
	{
		cout<<"Case #"<<i+1<<":\n";
		int output_n,amin,amax;
		cin>>output_n>>amin>>amax;
		int tmp=output_n;
		for(int ii=0;ii<j&&output_n!=0;++ii)
		{
			if(arr[b[ii]].age<=amax&&arr[b[ii]].age>=amin)
			{
				cout<<arr[b[ii]].name<<' '<<arr[b[ii]].age<<' '<<arr[b[ii]].worth<<'\n';
				--output_n;
			}
		}
		if(tmp==output_n) cout<<"None"<<'\n';
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值