看板 ck47th320 關於我們 聯絡資訊
※ 引述《changkh (留學生涯)》之銘言: : ※ 引述《genie2 (新挑戰)》之銘言: : : 一個簡單的問題 : : 我現在有一個檔案,大概有一兩千行(假設有N行) : : 每行都是像 : : 0 1 0 0 1 0 : : 這樣子0和1的格式 : : 每行的數字個數是固定的 : : 我現在想要把整個檔案的每一行亂數重排 : : 目前想到的方法是把整個檔案讀到int array裡面(用c) : : 然後再用一個產生好的1~N亂數sequence去把array裡的東西讀出來 : : 應該很staight forward : : 問題是有點煩 : : 有沒有人知道有什麼軟體可以直接完成這種事情的? : 你可以用 C++的STL來做。 : 把數字讀到vector裡面,之後用random_shuffle,就可以了。 試著寫寫看好了,沒有真的debug,所以當做參考吧。 #include <vector> #include <algorithm> #include <fstream> #include <iterator> using namespace std; int main() { vector<int> intvec; vector<int>::iterator pvec; int i; fstream ifile, ofile; ifile.open("input",ios::in); ofile.open("input", ios::out); while (!ifile.eof()) { 讀入一行,對該行的每個數字 { 取出數字,放在i intvec.push_back(i); } random_shuffle(intvec.begin(), intvec.end()); for (pvec=intvec.begin(); pvec != intvec.end(); pvec++) { ofile<<*pvec<<" "; } ofile<<endlk; intvec.clear(); } ifile.close(); ofile.close(); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 68.43.196.35