acm題目中有時測試樣例的輸入比較麻煩,一次次手動輸入肯定很費時間。所以用freopen函式,是一種比較快捷的辦法。
具體**是
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
事先應該在所在工程的原始檔處(右擊工程可以開啟所在資料夾),在這個資料夾裡面新建乙個名為 in 的文字文件,這個就作為輸入資料檔案,將要輸入的資料預先存入其中。
再在這個資料夾裡面新建乙個名為 out 的文字文件,這樣執行上面兩行**之後,輸出結果就會自動存入out.txt中。
進一步,在源**的開頭可以新增這樣一行**:
#define file
其中 file 可以是其他的名稱,然後在讀入讀出語句的開頭和結尾新增兩行**,如下:
#ifdef file
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
這樣就是告訴編譯器,如果我們事先定義了「file」,就執行freopen兩句,這個稱為「活動預處理模組」。
以乙個很簡單的問題為例:輸入16進製制數,將其改寫為10進製
用檔案讀入讀出的**如下:
#include#include#define file
using namespace std;
int main()
{#ifdef file
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
char data16[400];
int data10=0,d;
int i;
/**********program**********/
int j=0;
while(cin>>data16)
{ int num16[100];
d=strlen(data16);
int power[100];
for(i=0;i
讀入讀出優化
有些題目追求高效能 比如說 nlogn 這個時候根據情況使用線段樹 樹狀陣列 記憶化搜尋或者搜尋剪枝很重要,但是不要忘記快讀 一般來說scanf和printf的速度已經很令人滿意了,雖說比不上pascal裡面的read 在讀入大量資料的時,比如10 7個int型的整數,scanf也顯得力不從心 快讀...
python檔案的讀入讀出重新命名
import os 匯入檔案頭 fo open c users lys desktop 1.txt w 用w 覆蓋重寫的方法開啟檔案 fo.write hello world 往裡面寫入字串 position fo.seek 0,0 把指標移到最前面 string fo.readline 讀入一行字...
cookie的讀入和讀出
寫入cookie中 在mvc的控制器中 httpcookie getuserid new httpcookie uid 要儲存的值 getuserid.expires datetime.now.adddays 7 設定過期時間 this.response.cookies.add getuserid ...