gha890826

0604-bonus 二分搜尋法

Jun 24th, 2021 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. //這根本不是二分 只是題目叫二分不要參考
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int n,q,num=0;
  10.  
  11. int main ()
  12. {
  13.   while(cin>>n>>q)
  14.   {
  15.       if(n!=0&&q!=0)
  16.       {
  17.           num++;
  18.           cout<<"CASE # "<<num<<":\n";
  19.           int in[n];
  20.           for(int i=0;i<n;i++)
  21.           {
  22.               cin>>in[i];
  23.           }
  24.           sort(in,in+n);
  25.           int sear;
  26.           for(int i=0;i<q;i++)
  27.           {
  28.               cin>>sear;
  29.               bool find=0;
  30.               for(int j=0;j<n;j++)
  31.               {
  32.                   //cout<<in[j]<<endl;
  33.                   if(in[j]==sear)
  34.                   {
  35.                       cout<<sear<<" found at "<<j+1<<endl;
  36.                       find=1;
  37.                       break;
  38.                   }
  39.               }
  40.               if(!find)
  41.               {
  42.                   cout<<sear<<" not found\n";
  43.               }
  44.           }
  45.          
  46.       }
  47.       else
  48.         return 0;
  49.   }
  50. }
Add Comment
Please, Sign In to add comment