Question Link: UVA10474 Where is the Marble?.
Test Instructions Description: Enter n integers, representing the marble number, and then enter Q number (number), ask if there is this number of marble, where is the location?
This problem is written in the C + + language program, mainly to practice the use of STL functions.
The program uses two functions in the algorithm library (algorithm), uses the sort () function to sort the data, the function's parameters are simpler than the similar functions of the C language, the program is easier to write, and the function Lower_bound () to find the elements is simple and convenient.
The AC C + + language program is as follows:
/* UVA10474 Where is the Marble? */#include <iostream> #include <algorithm>using namespace std; #define MAXN 11000int Marble[maxn];int Main () { int n, Q, Caseno=0, Val; while (scanf ("%d%d", &n, &q)! = EOF) { if (n = = 0 && q = = 0) break ; for (int i=0; i<n; i++) scanf ("%d", &marble[i]); Sort (marble, marble + N); printf ("case#%d:\n", ++caseno); while (q--) { scanf ("%d", &val); int no = Lower_bound (marble, Marble + N, Val)-Marble; if (marble[no] = = val) printf ("%d found at%d\n", Val, no + 1); else printf ("%d not found\n", Val); } } return 0;}
UVA10474 Where is the Marble?