[AOJ 문제풀이] 짝맞추기

문제 링크 : http://algospot.cafe24.com/zbxe/?mid=aoj&action=problem&no=100

제출한 답안 :

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

#define SWAP(a,b) { int t;t=a;a=b;b=t; }

void QuickSort(int *ar, int num)
{
     int left,right;

     int key;

     if (num <= 1) return;

     key=ar[num-1];

     for (left=0,right=num-2;;left++,right--) {

          while (ar[left] < key) { left++; }

          while (ar[right] > key) { right--; }

          if (left >= right) break;

          SWAP(ar[left],ar[right]);

     }

     SWAP(ar[left],ar[num-1]);

     QuickSort(ar,left);                           // 왼쪽 구간 정렬

     QuickSort(ar+left+1,num-left-1);        // 오른쪽 구간 정렬

}


int main(void)
{
 
 int ncase, num, result = 0, i = 0, j;
 int *m, *w;

 cin >> ncase;

 while(i < ncase)
 {
  cin >> num;

  m = new int[num];
  w = new int[num];

  for(j = 0; j < num; j++)
   cin >> m[j];

  for(j = 0; j < num; j++)
   cin >> w[j];

  QuickSort(m, num);
  QuickSort(w, num);
  
  for(j = 0; j < num; j++)
   result += abs(m[j] - w[j]);

  cout << result << endl;

  result = 0;

  delete [] m;
  delete [] w;

  i++;
 }
 

 return 0;
}


aoj_100.cpp

by 코난도일 | 2009/01/15 00:47 | 코딩 실습 | 트랙백 | 덧글(0)

트랙백 주소 : http://drshin.egloos.com/tb/2247044
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶