[問題] 台中女中程式解題系統:b005: 熱門點播

看板C_and_CPP (C/C++)作者 (bom)時間13年前 (2012/08/13 22:49), 編輯推噓2(206)
留言8則, 3人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) windows XP + dev-c++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b005 輸入說明: 一開始有一個正整數 N (1<=N<=20),代表有幾張明信片的點播,接下來有 N 個 1~100000000 的正整數,代表這 N 張明信片所點播的歌曲編號。 輸出說明: 請輸出點播次數最高的歌曲編號,以及它的點播次數(不用考慮最高點播次數的歌曲有兩 首以上的情況),中間請空一格。 餵入的資料(Input): 3 10 20 20 預期的正確結果(Expected Output): count[1][0]= 10, count[1][1]=1 count[2][0]= 20, count[2][1]=2 錯誤結果(Wrong Output): temp[1]=10 count[1][0]= 10, count[1][1]=1 //這裡就出現問題了, 為何不是 count[2][0] temp[2]=20 count[3][0]= 20, count[3][1]=1 temp[3]=20 count[3][0]= 20, count[3][1]=2 //這裡 count[2][0]等於1, count[2][1]等於20 也看不出來是哪裡出錯 count[1][0]= 10, count[1][1]=1 count[2][0]= 1, count[2][1]=20 count[3][0]= 20, count[3][1]=2 程式碼(Code):(請善用置底文網頁, 記得排版) 附上code http://codepad.org/MrGGPqIk #include <stdio.h> int main(void){ int N,t,i,j; int count[21][1]={0},temp[21]; //count[i][0] 是編號, count[i][1]是次數, //由count[1]開始 scanf("%d", &N); //先輸入到temp array for(t=1;t<=N;t++){ scanf(" %d", &temp[t]); } for(t=1;t<=N;t++){ printf("temp[%d]=%d\n",t,temp[t]); //把temp 放到 count array 裡 for(i=1;i<=N;i++){ //如果count[i][0]等於temp[t], 則count[i][1] +1 if(count[i][0] == temp[t]){ count[i][1] = count[i][1]+1; printf("count[%d][0]= %d, count[%d][1]=%d\n", i, count[i][0], i, count[i][1]); break; } //如果count 找不到等於temp[t],也就是temp[t]是首次出現, //則找第一個 count[i][0] 等於 0 的地方來儲存 temp[t] if(count[i][0] == 0) { count[i][0] = temp[t]; count[i][1] = 1; printf("count[%d][0]= %d, count[%d][1]=%d\n", i, count[i][0], i, count[i][1]); break; } } } for(i=1;i<=N;i++){ printf("count[%d][0]= %d, count[%d][1]=%d\n", i, count[i][0], i, count[i][1]); } system("PAUSE"); return 0; } debug 很久看不出來哪裡出問題,請大家幫忙,謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.127.201.97

08/13 23:16, , 1F
那個count定義沒問題嗎?
08/13 23:16, 1F

08/13 23:22, , 2F
Line14那個for有問題喔,你可能要想清楚那邊如果那樣寫
08/13 23:22, 2F

08/13 23:22, , 3F
會怎麼運作
08/13 23:22, 3F

08/13 23:30, , 4F
你的i怎麼會是從1開始
08/13 23:30, 4F

08/13 23:30, , 5F
array的元素從0開始的啊,老弟…
08/13 23:30, 5F

08/13 23:54, , 6F
我有註明count由 1 開始
08/13 23:54, 6F

08/13 23:54, , 7F
可否請問line14 for 問題出在哪裡?因為 temp[i]好像都OK
08/13 23:54, 7F

08/14 00:18, , 8F
你這樣寫是自討苦吃…另外你的問題有人回了
08/14 00:18, 8F
文章代碼(AID): #1GAHFiHK (C_and_CPP)
文章代碼(AID): #1GAHFiHK (C_and_CPP)