[問題] 台中女中程式解題系統:b005: 熱門點播
開發平台(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
08/13 23:16, 1F
→
08/13 23:22, , 2F
08/13 23:22, 2F
→
08/13 23:22, , 3F
08/13 23:22, 3F
推
08/13 23:30, , 4F
08/13 23:30, 4F
→
08/13 23:30, , 5F
08/13 23:30, 5F
→
08/13 23:54, , 6F
08/13 23:54, 6F
→
08/13 23:54, , 7F
08/13 23:54, 7F
推
08/14 00:18, , 8F
08/14 00:18, 8F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章