[討論] c++ qsort 的問題

看板C_and_CPP作者 (LONGTED)時間11年前 (2014/09/21 02:47), 編輯推噓1(104)
留言5則, 4人參與, 最新討論串1/1
請問一個 我有一個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

09/21 02:50, , 1F
因為陣列從 0 開始...
09/21 02:50, 1F

09/21 03:33, , 2F
我知道 我是要問1-N 要如何qsort
09/21 03:33, 2F

09/21 04:03, , 3F
qsort(student_ans+1,n+1,......)
09/21 04:03, 3F

09/21 05:16, , 4F
qsort(student_ans+1, n,......)
09/21 05:16, 4F

09/21 13:30, , 5F
scwg 的才對 XD
09/21 13:30, 5F
文章代碼(AID): #1K7SkaAc (C_and_CPP)