티스토리 뷰

Test/C

center number (크기 기준)

kiostory 2019. 4. 28. 22:48

크기 기준으로 정렬하고 가운데 수를 구한다.

(데이터가 홀수개의 경우)


짝수개의 경우에는 중앙 바로 앞 수를 구했음



--------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(void)
{
 int Array[5]={5,1,8,11,9};
 // 데이터들 int Array[]={-22,-59,5,1,3,4};
 // 데이터들 int Array[]={-22,-59,33,27,3,65,5,1,3,3,4,11,9,23,48,22,-22};


 int TmpArray[20]={0,};

 int count1 = sizeof(Array) / sizeof(int);
 memcpy(TmpArray, Array, sizeof(int)*count1);


 int i=0;
 int j=0;
 int Temp=0;
 for (i=0;i<=(count1-2);i++){
  for (j=i+1;j<=(count1-1);j++){
   if (TmpArray[i]<TmpArray[j]){
    Temp=TmpArray[j];
    TmpArray[j]=TmpArray[i];
    TmpArray[i]=Temp;
   }
  }
 }

 printf("[초기 입력 데이터 ]: \n");
 for (int a=0;a<=count1-1;a++){
 printf("%d ",Array[a]);
 }
 printf("\n");

 printf("[ASC Sorting 데이터 ]: \n");
 for (int b=0;b<=count1-1;b++){
 printf("%d ",TmpArray[b]);
 }
 printf("\n");

 if (count1 % 2 == 1) count1=count1/2;
 else count1=count1/2-1;
 printf("[중앙수 ]: %d\n",TmpArray[count1]);



 return 0;

}
------------------------------

결과


[초기 입력 데이터 ]:
5 1 8 11 9
[ASC Sorting 데이터 ]:
11 9 8 5 1
[중앙수 ]: 8

'Test > C' 카테고리의 다른 글

cross HAP  (0) 2019.05.01
Dedup  (0) 2019.05.01
2018_2_pad  (0) 2019.04.27
isdigit(), sprintf(), atoi()  (0) 2019.04.16
MingGW 컴파일러 데이터타입 싸이즈  (0) 2019.04.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함