看板 C_and_CPP 關於我們 聯絡資訊
請問一個 我有一個array 1 到n 我呼叫qsort 這個內建的函式 可是sort 出來 好像是 3-n 排序過 array[1] array[2] 是一個有的沒有的數 我是哪裡寫錯了 #include<stdio.h> #include<algorithm> #define MAX_SIZE 512 int compare(const void *a,const void *b) { int *ta=(int *)a; int *tb=(int *)b; if(*ta>=*tb) return 1; return -1; // return ( *(int*)a - *(int*)b ); } int _tmain(int argc, _TCHAR* argv[]) { int n; scanf("%d",&n); int correct_ans[MAX_SIZE]={0}; int student_ans[MAX_SIZE]={0}; for(int i=1;i<=n;i++) scanf("%d",&student_ans[i]); qsort(student_ans,n+1,sizeof(student_ans),compare); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.168.187.223 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1411238820.A.2A6.html
LPH66: 因為陣列從 0 開始... 09/21 02:50
longted3: 我知道 我是要問1-N 要如何qsort 09/21 03:33
CindyLinz: qsort(student_ans+1,n+1,......) 09/21 04:03
scwg: qsort(student_ans+1, n,......) 09/21 05:16
CindyLinz: scwg 的才對 XD 09/21 13:30