编辑代码

#include <iostream>
using namespace std;

struct Location{
    string name;
    int time;
    int score;
};

int pow(int num,int i){
    for(int j=0;j<i-1;j++){
        num=num*2;
    }
    return num;
}

void getMaxScoreForLocation(int totalTime,struct Location Loc[], int locationCount) {

    int maxScore = -1;
    int maxScorePos = -1;

    for (int i = 0; i < pow(2,locationCount); i++) {

        int pos = i;
        int time = 0;
        int score = 0;
        int curBit = 0;

        while (pos > 0) {
           
            if (pos & 1 == 1) {
                time += Loc[curBit].time;
                score += Loc[curBit].score;
            }
            pos = pos >> 1;
            ++curBit;
        }

        if (time <= totalTime && maxScore < score) {
            maxScore = score;
            maxScorePos = i;
        }
    }

    if (maxScorePos > 0) {
        int curBit = 0;
        while (maxScorePos > 0) {
            if (maxScorePos & 1 == 1) {
               cout << Loc[curBit].name << "(" << Loc[curBit].time << "," << Loc[curBit].score << ")" << endl;
            }
            maxScorePos = maxScorePos >> 1;
            ++curBit;
        }
        cout << "max score:" << maxScore << endl;
    }
}

int main() {
    struct Location loc[4]={
        "故宫", 1, 7,
        "颐和园", 2, 8,
        "长城", 3, 9,
        "天坛", 1, 6
    };

    getMaxScoreForLocation(4,loc,4);

	return 0;
}