5.1 Exercises --- marble algorithm in which (UVa10474)

A: Title

N existing marble, each written on a marble non-negative integer, each of the first number of small to large order, then Q answer questions. 
Each question asks whether there is a marble that says some integer x, if so, on which even answer that says x marble.
Sorted marble numbered from left to right. 1
~ N. (In the sample, in order to save space, the number of all combined into one row marble, all problems also be incorporated into a line.)

(A) Sample Input

4 1
2 3 5 1
5
5 2
1 3 3 3 1
2 3

(B) Sample Output

CASE# 15 found at 4
CASE# 22 not found
3 found at 3 

Two: code implementation

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX_LEN 1000
int a[MAX_LEN];

int main()
{
    FILE* fp = freopen("data5_1.in", "r", stdin);
    freopen("data5_1.out", "w", stdout);
    int i = 0,num,c,val,pos;

    while (!feof(fp))
    {
        //Input message 
        COUT << " the CASE # " << << I ++ " : " << endl;
         // get the initial information 
        CIN NUM >> >> C;
         for ( int J = 0 ; J <NUM; J ++ ) 
            CIN >> A [J];
         // sort 
        sort (a, a + num) ; // close look at the left and right open source is less default ascending sort
         // obtaining query information 
        for ( int J = 0 ; J <C ; J ++ ) 
        { 
            CIN >> Val;
            post= lower_bound(a, a + num, val) - a;
            if (a[pos] == val)
                cout << val << " found at " << pos + 1 << endl;
            else
                cout << val << " not found" << endl;
        }
    }

    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    return 0;
}

(A) Method Sort

sort comparison operator using the default size of the array is sorted. Its collation can be customized as needed. 
Sort (A, A + n-) can array a [n] be the default ordering
Which is more than can sort the array to sort, he can sort any object, because the sort is a template function

(Ii) Method lower_bound

lower_bound for finding x greater than or equal to a first position

 

Guess you like

Origin www.cnblogs.com/ssyfj/p/11512131.html