算法競賽中的輸入輸出框架

一、輸入一些數

while(scanf("%d %d",&x,&y)==2)
		cout<<x<<' '<<y;

輸入完畢之後先按Enter,再按ctrl+Z,最後再按Enter鍵,即可結束輸入。Linux下,輸入完畢後按ctrl+D即可結束輸入。

二、文件

  1. 輸入輸出重定向方法
#define LOCAL
int main()
{
	#ifdef LOCAL
		freopen("data.in","r",stdin);
		freopen("data.out","w",stdout);
	#endif
	int x,y;
	scanf("%d",&x);
	printf("%d",x);
	return 0;
}

scanf從文件data.in讀入,printf寫入文件data.out;
重定向部分被寫入#ifdef,#endif中,只有定義了符號LOCAL,才編譯這兩條freopen語句

  1. 文件輸入輸出
//#define LOCAL
int main()
{
	#ifdef LOCAL
		freopen("data.in","r",stdin);
		freopen("data.out","w",stdout);
	#endif
	FILE *fin,*fout;
	fin=fopen("data.in","rb");
	fout=fopen("data.out","wb");
	int x,y;
	fscanf(fin,"%d",&x);
	fprintf(fout,"%d",x);
	fclose(fin);
	fclose(out);
	return 0;
}
  1. 標準輸入輸出
    想把fopen版的程序改成讀寫標準輸入輸出,只需賦值fin=stdin,fout=stdout即可,不要調用fopen和fclose
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章