A FLAG # 13 # V-shaped sequence

question

See reference [1] for the complete subject.

#include <iostream> 
#include <cstdio> using namespace std; int main () 
{ int T, n, num; // Number of data sets, number of commodities, number of commodities float price, sum; // A kind of commodities Price, the total price of a group of data     
    cin >> T;    
     while (T-- ) { 
        sum = 0 ; 
        cin >> n;
         for ( int i = 0 ; i! = N; ++ i) { 
            scanf ( " % f% d " , & price, & num); //

 


    
    
double is% lf 
            sum + = price * num; 
        } 
        printf ( " % .2f \ n " , sum); // double is% .2lf 
    } 
    
    return  0 ; 
}

 

V-shaped sequence-exhaustive method. See reference [3] for the complete subject.

#include <iostream> 
#include <cstdio> using namespace std; const int MAXN = 20 ;
 int a [MAXN]; int main () 
{ int T; // Number of data sets 
    cin >> T;
     while (T-- ) {
         int n, cnt = 0 ; // The length of the sequence, the number of v-shaped sequence 
        scanf ( " % d " , & n);
         if (n < 3 ) { 
            printf ( " cnt: 0 \ n "

 

 


    );
            continue;
        }
        for (int i = 0; i != n; ++i) {
            scanf("%d", a + i);    // 同 &a[i] 的写法    
        }
        
        int i = 0, j = 1, k = 2;
        for (int i = 0; i != n - 2; ++i) {
            for (int j = i + 1; j != n - 1; ++j) {
                for (int k = j + 1; k != n; ++k) {
                    if (a[i] > a[j] && a[j] < a[k]) cnt++;
                }
            }
        }
        
        printf("cnt: %d\n", cnt);
    }
    return 0;
}

 

V-shaped sequence-still exhaustive.

        int i = 0, j = 1, k = 2;
        for (int i = 0; i != n - 2; ++i) {
            for (int j = i + 1; j != n - 1; ++j) {
                if (a[i] > a[j]) {
                    for (int k = j + 1; k != n; ++k) {
                        if (a[j] < a[k]) cnt++;
                    }                    
                }
            }
        }

 

reference

[1]  [Title] Problem A. Supermarket Checkout _Java_Dark Dream Factory-CSDN Blog

[2] Is  it better to define variables inside or outside the loop?

[3]  [Title] Problem BV font sequence _Java_Dark Dream Factory-CSDN Blog

 

Guess you like

Origin www.cnblogs.com/xkxf/p/12673546.html