카테고리 없음

10474 - Where is the Marble?

홍열 2012. 9. 29. 23:30
728x90

이 문제에서 왜 저딴 배경지식을 써놨는지는 모르겠다.


하지만 그냥 Sort하고 해당 값의 위치를 출력하면 끝인 문제이다.


가끔 UVA에 이런 문제가 있는 듯 하다.


input을 보면 

4 1

2

3

5

1

5


인데 처음 4개가 기준 배열이고 뒤에 1개가 찾을 값이다.

해당 값이 있으면 위치 출력하고 없으면 not found 출력하면 된다.



소스코드


  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int compare (const void * a, const void * b)
  7. {
  8.   return ( *(int*)- *(int*));
  9. }
  10.  
  11. int main()
  12. {
  13.     int t_case, f_case, cnt;
  14.    
  15.     bool flag = false;
  16.     cnt = 1;
  17.     while(cin>>t_case>>f_case)
  18.     {
  19.         if(t_case == 0 && f_case == 0) break;
  20.  
  21.         int *arr = new int[t_case];
  22.         int *arr2 = new int[f_case];
  23.  
  24.         for(int i = 0; i<t_case; i++)
  25.         {
  26.             cin>>arr[i];
  27.         }
  28.         for(int i =0; i<f_case; i++)
  29.         {
  30.             cin>>arr2[i];
  31.         }
  32.         qsort (arr, t_case, sizeof(int), compare);
  33.        
  34.         cout<<"CASE# "<<cnt++<<":"<<endl;
  35.         for(int i =0; i<f_case; i++)
  36.         {
  37.            
  38.             flag = false;
  39.             for(int j = 0; j<t_case; j++)
  40.             {
  41.                 if(arr2[i] == arr[j]) {
  42.                     cout<<arr2[i]<<" found at "<<j+1<<endl;
  43.                     flag = true;
  44.                     break;
  45.                 }
  46.             }
  47.             if(flag == false) cout<<arr2[i]<<" not found"<<endl;
  48.  
  49.         }
  50.  
  51.  
  52.     }
  53.    
  54.     return 0;
  55. }