[Offer收割]编程练习赛4 register

Ended

Participants:899

Verdict:Accepted
Score:100 / 100
Submitted:2016-08-07 12:32:40

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool findLeastPrice(vector<int> &prices, int thisPos, int nowPrice, int X, int &leastPrices){
    if (nowPrice == X) {
        leastPrices = X;
        return true;
    }
    
    if (nowPrice > X && nowPrice < leastPrices) {
        leastPrices = nowPrice;
        return false;
    }
    
    for (int i = thisPos; i < prices.size(); i++) {
        nowPrice += prices[i];
        if(findLeastPrice(prices, i+1, nowPrice, X, leastPrices)){
            return true;
        }else{
            nowPrice -= prices[i];
        }
    }
    
    return false;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX