ACM竞赛做题的输入输出惯用伎俩(1)
时间:2010-12-15 来源:jinhaoxia
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
意思是要有首先输入一个T,然后输入T行测试样本。我们以
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2970
为例子。为了保留这些测试样本,有那门一种做法:
#include <iostream> #include <string.h> main() { using namespace std; int times; cin>>times; while(times--) { char type[10]; int many; int get; int most; cin>>type; cin>>many; if(strcmp(type,"Faster")==0) { most=0x7fffffff; while(many--) { cin>>get; if(get<most) { most=get; } } } else { most=0; while(many--) { cin>>get; if(get>most) { most=get; } } } cout<<most<<endl; } }
对于这种题目都有一个固定框架,那就是:
#include <iostream> #include <string.h> main() { using namespace std; int times; cin>>times; while(times--) { //什么测试实例 //输入测试实例 //处理 //输出 } }
要理解这种写法,要首先理解C的文件机制,显示器和键盘对C来说不过就是文件,用户端的输入输出或许交错在一起,其实他们都在他们的缓冲区中单独存在。
相关阅读 更多 +