此题限时,如果用的c++的话,用cin输入相对于scanf会消耗更多的时间,消除的方式是在main()函数开始的时候加上一句 ios::sync_with_stdio(false); 这样可以使cin和scanf的效率相差甚小。
如果用c则木有这个问题
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int num1,num2;
int main()
{
ios::sync_with_stdio(false);
int n,x;
cin>>n;
int a[101] = {0};
while(n--){
cin>>x;
if(x>100)//再一点需要注意的就是大于100岁,按100岁来算
x = 100;
a[x]++;
}
for(int i=0; i<=100; i++){
if(a[i])
cout<<i<<" "<<a[i]<<endl;
}
return 0;
}
本文介绍在C++中如何通过ios::sync_with_stdio(false)优化IO操作,使其接近scanf效率,适用于限时编程竞赛。并展示了一个年龄统计的例子,包括输入处理、年龄范围校验及结果输出。
349

被折叠的 条评论
为什么被折叠?



