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