#UVa:10474-Where is the Marble?

對數字做排序再搜尋即可得解。

C++(0.119)

/*******************************************************/
/* UVa 10474 Where is the Marble?                      */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2015/05/04                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
  int caseNumber = 0;
  int N, Q;
  int marbles[10005];
  while( scanf("%d%d", &N, &Q) != EOF && N != 0 && Q != 0 ){
    printf("CASE# %d:\n", ++caseNumber);
    for( int i = 0 ; i < N ; ++i ){
      scanf("%d", &marbles[i]);
    }

    sort(marbles, marbles+N);

    int query;
    for( int i = 0 ; i < Q ; ++i ){
      scanf("%d", &query);
      int *position = lower_bound( marbles, marbles+N, query );
      if( position == marbles+N || *position != query ){
        printf("%d not found\n", query);
      }
      else{
        printf("%d found at %d\n", query, (int)(position-marbles)+1);
      }
    }
  }

  return 0;
}

發表迴響