Based on the review --STL unique, bound, greatest common factor

unique

#include <cstdio> 
#include <algorithm> 
#include <the iostream>
 the using  namespace STD;
 int A [] = { . 1 , . 3 , . 3 , . 3 , . 5 , . 6 , 0 , 0 , . 8 , . 7 , . 8 };
 int main () { 

    // UNIQUE (iter1, iter2), left and right to open and close, returned is "last address" after deduplication -> pointer
     // different attention and end address of the original array
     // size of the original array, and no change
     // UNIQUE function just repeating elements placed behind the original array
     // deduplication element refers to a continuously repeated
    // so often array row again after re-sorted, so you can remove all of the elements repeatedly 
    int SZ = UNIQUE (A, A + the sizeof (A) / the sizeof ( int )) - A;
     for ( int I = 0 ; I <SZ; ++ I) { 
        COUT << A [I] << '  ' ; 
    } COUT << endl; 
    COUT << SZ << endl;
     return  0 ; 
}

 

bound

/ * 
Lower_bound and upper_bound principles are binary search, the efficiency is very high 
return address elements of the "first" to meet the conditions: 
    Note intervals are "closed left and right on"! 
    upper_bound (begin, end, num) - a first num strictly greater than 
    the first or less num - lower_bound (begin, end, num) 
    can be obtained index by subtracting the first address element 
or end the lookup fails ( ) 
using the following general sorted 
* / 
#include <algorithm> 
#include <cstdio> 
#include <the iostream> 
#include <the cstdlib>
 the using  namespace STD; 

int main () {
     int A [] = { . 1 , 2 , . 7 , 3 , 15 , 34 };
    sort(a, a+sizeof(a)/sizeof(int));
    int pos = lower_bound(a, a+sizeof(a)/sizeof(int), 7) - a;
    cout << a[pos] << endl;

    pos = upper_bound(a, a+sizeof(a)/sizeof(int), 7) - a;
    cout << a[pos] << endl;
    return 0;
}

 

gcd

/ * Removed division && Decreases * / 
#include <cstdio> 
#include <the iostream>
 the using  namespace STD; 

// Decreases, attention proviso A> = B 
/ * 
for example to understand: 
    Suppose two ball the number of the greatest common divisor of 161 and 63, m is a solution set 
    larger 161 can be regarded as 63 + 98, 63 because it is divisible m-- 98 must also divisible m 
    problem into seeking the greatest common divisor of 63 and 98 

    At this point 98 can be seen as 63 + 35 -> 63 and the greatest common divisor for the sake of conversion of 35 
    ............ 
    last conversion for the sake of the greatest common divisor of 7 and 7 
* / 
int F ( int a, int B) {
     IF (A < B) the swap (A, B);
     int R & lt A- = B;
     IF (R & lt == 0 ) returnA;
     IF (R & lt> b) return F (R & lt, b);
     the else  return F (b, R & lt); 
} 

// solving a, the greatest common divisor of b
 @ removed division: in fact, when Decreases Variant 
/ * 
during the presentation Decreases may be found in the upper side 
find a and b (a> b) the greatest common factor is continuously reduced b, until it stops subtraction 
-> a% b is equivalent to a final remainder obtained 
-> This will be removed to optimize divided Decreases 
* / 
// recursive version 
int gcd_recursion ( int A, int B) {
     IF (A < B) the swap (A, B);
     return (B == 0 ?) A: gcd_recursion (b, A% b); 
} 
// loop version 
int gcd_loop ( int a, int b) {
    if(a < b) swap(a, b);
    int r;
    while(b !=0) {
        r = a%b;
        a = b;
        b = r;
    }
    return a;
}
int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("gcd = %d %d %d\n", gcd_loop(a, b), gcd_recursion(a, b), f(a, b));
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/GorgeousBankarian/p/11406955.html