POJ 入门
先復(fù)習(xí)一下C的一些基本概念
1、C標(biāo)準(zhǔn)化輸出:scanf
int m,n; scanf("%d%d",&n,&m);實(shí)際上scanf是有返回值的,且返回值的類型為int,為輸入的個(gè)數(shù)。如:
int m,n; printf("%d", scanf("%d%d",&n,&m) );//輸入 12 56 //輸出 2//輸入 2 a a輸入失敗 //輸出 1//輸入 a 5 a輸入失敗,則后面的也失敗,故輸出為0 //輸出 0scanf還有一個(gè)返回值EOF(即-1,符號(hào)常量),代表輸入數(shù)據(jù)已經(jīng)結(jié)束,如:
#include <iostream> using namespace std;int main() {int a,b;while(scanf("%d%d",&a,&b) != EOF){printf("%d\n",a+b);}return 0; }在Windows下,按Ctrl+z,再按回車即可結(jié)束輸入。
?
?
?
2、C++的標(biāo)準(zhǔn)化輸出:cin
cin表達(dá)式的值,只能為true(成功讀入所有變量) 和false
對(duì)應(yīng)的一直輸入為:
#include <iostream>using namespace std;int main() {int a,b;while(cin>>a>>b) // 注意這里不能加;否則不執(zhí)行后面的 {cout << a+b << endl;}return 0; }Windows停止同按Ctrl+z? 回車
?
?
例如:輸入若干個(gè)(不知道多少個(gè))正整數(shù),輸出其中的最大值
#include <iostream> using namespace std;int main() {int a,mx=0;while(scanf("%d",&a) != EOF){if (a>mx){mx = a;}printf("%d\n",mx);}return 0; }?
?
?
?
?
?
?
3、用freopen重定向輸入
調(diào)試程序時(shí),每次運(yùn)行程序都要輸入測(cè)試數(shù)據(jù),太麻煩
可以將測(cè)試數(shù)據(jù)存入文件,然后用freopen將輸入由鍵盤重定向?yàn)槲募?#xff0c;則運(yùn)行程序時(shí),就不需要輸入數(shù)據(jù)了。:
#include <iostream> using namespace std;int main() {freopen("E:\\CodeBlocks\\Project_\\POJ\\test1\\input.txt","r",stdin); // \\為轉(zhuǎn)義\ 且注意交到oj上的時(shí)候注意把它注釋掉// 此后所有輸入都來自文件 input.txtint a,mx=0;while(scanf("%d",&a) != EOF){if (a>mx){mx = a;}}printf("%d\n",mx);return 0; }input文件為:
運(yùn)行結(jié)果為:
?
轉(zhuǎn)載于:https://www.cnblogs.com/cymwill/p/9277313.html
總結(jié)
- 上一篇: Linux并发与同步专题 (1)原子操作
- 下一篇: ArcObjects中的几何对象简介(一