看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) xcode 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 在讀入的數量加減幾個數得出的結果都正確 但用亂數提供數據或是數據提供太多結果會錯誤 餵入的資料(Input): 1 2 1 6 2 3 2 5 2 6 3 4 5 3 5 4 6 5 預期的正確結果(Expected Output):s fn[1]=1 an[1]=2 fn[2]=1 an[2]=6 fn[3]=2 an[3]=3 fn[4]=2 an[4]=5 fn[5]=2 an[5]=6 fn[6]=3 an[6]=4 fn[7]=5 an[7]=3 fn[8]=5 an[8]=4 fn[9]=6 an[9]=5 a[1]=2 a[2]=3 a[3]=1 a[4]=0 a[5]=2 a[6]=1 b[1]=1 b[2]=3 b[3]=6 b[4]=7 b[5]=7 b[6]=9 b[7]=10 錯誤結果(Wrong Output): 從a[2],b[2]開始錯誤 程式碼(Code):(請善用置底文網頁, 記得排版) using namespace std ; int main() { ofstream ofile("/Users/answer.txt"); int fn[link + 1], an[link + 1]; int i; ifstream ifile("/Users/test.txt"); for (i = 0; i < link; i++) { ifile >> fn[i]>> an[i]; ofile<< "fn[" << i + 1 << "]=" << fn[i] << " "; ofile << "an[" << i + 1 << "]=" << an[i] << endl; cout<< "fn[" << i + 1 << "]=" << fn[i] << " "; cout << "an[" << i + 1 << "]=" << an[i] << endl; } ofile << " "<< endl; cout << " "<< endl; int a[node+1],j; for (j=1; j<=node; j++) { for (i = 0; i < link; i++) { if(fn[i]==j) a[j]+=1; } ofile <<"a[" << j << "]=" << a[j]<<endl; cout <<"a[" << j << "]=" << a[j]<<endl; } ofile << " "<< endl; cout << " "<< endl; int k,b[i]; b[i]=1; ofile << "b[" << 1 << "]=" << b[i]<< endl; cout << "b[" << 1 << "]=" << b[i]<< endl; for (k=1 ; k<=6; k++) { b[i] = b[i]+a[k]; ofile << "b[" << k+1 << "]=" <<b[i]<< endl; cout << "b[" << k+1 << "]=" <<b[i]<< endl; } } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.159.227 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1444017180.A.16E.html
johnjohnlin: 那個 int a[node+1],看起來是置底13-1 10/05 15:24