[問題] 重新排序並且記錄排序前的位置
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
C
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
no
問題(Question):
將餵入的array中數字做排序之後
將由小到大的數字依照原本array中的位置輸出
餵入的資料(Input):
A[10]={4,1,7,8,3,0,0,9,2,6};
預期的正確結果(Expected Output):
6,7,2,9,5,1,10,3,4,8
錯誤結果(Wrong Output):
7,0,2,9,5,1,10,3,4,8
程式碼(Code):(請善用置底文網頁, 記得排版)
http://ideone.com/c6Let
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int comp_func(const void* a, const void* b)
{
return (*(int*)a-*(int*)b);
//return( strcmp((char *)a,(char *)b) );
}
int main()
{
int a,b,c,d;
int i,j;
int count=5;
int A[10]={4,1,7,8,3,0,0,9,2,6};
int B[10]={0};
int C[10]={0};
int M[10]={0};
int num=10;
for(i=0;i<num;i=i+10)
{
for(j=0;j<10 && i+j <num;j++)
{
B[j]=A[i+j]; //printf("%d\n ",j);
}
qsort((void*)B, 10, sizeof(*B), comp_func);
for (a=0;a<10;a++)
{
for(b=0;b<10;b++)
{
if(B[a]==A[b]&&M[b]==0)
{
C[a]=b+1;
M[b]=M[b]+1;
}
else
{
continue;
}
}
}
for (j=0;j<10;j++)
{
printf("%d = %d\n",j+1,C[j]);
}
}
補充說明(Supplement):
無
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.200.17
※ 編輯: siv 來自: 140.114.200.17 (06/25 19:07)
※ 編輯: siv 來自: 140.114.200.17 (06/25 19:09)
→
06/25 20:31, , 1F
06/25 20:31, 1F
→
06/25 20:32, , 2F
06/25 20:32, 2F
→
06/25 20:33, , 3F
06/25 20:33, 3F
→
06/25 20:36, , 4F
06/25 20:36, 4F
→
06/25 21:02, , 5F
06/25 21:02, 5F
→
06/25 21:05, , 6F
06/25 21:05, 6F
→
06/25 21:05, , 7F
06/25 21:05, 7F
→
06/25 21:06, , 8F
06/25 21:06, 8F
→
06/25 21:06, , 9F
06/25 21:06, 9F
→
06/25 21:07, , 10F
06/25 21:07, 10F
→
06/25 21:38, , 11F
06/25 21:38, 11F
→
06/25 21:39, , 12F
06/25 21:39, 12F
→
06/25 21:39, , 13F
06/25 21:39, 13F
→
06/25 21:42, , 14F
06/25 21:42, 14F
→
06/25 21:45, , 15F
06/25 21:45, 15F
→
06/25 21:46, , 16F
06/25 21:46, 16F
→
06/25 21:47, , 17F
06/25 21:47, 17F
→
06/25 21:47, , 18F
06/25 21:47, 18F
→
06/25 21:48, , 19F
06/25 21:48, 19F
→
06/25 21:48, , 20F
06/25 21:48, 20F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章