洛谷 p1080国王游戏 题解

国王游戏


#include
#include
#include
#include 
#include 

/*P1080 国王游戏
输入
1 大臣数n
2 国王左手数 国王右手数
3-n+2 大臣左手数 大臣右手数

 输出
 获得金币最多的大臣获得金币数量
 */

using namespace std;

int king_left = 0, king_right = 0;
int minister_number = 0;
//int max_gold = 0;
int a_front[10005] = {0};
//int mem[1005] = {0};
int *max_gold = new int[5005];
int max_length = 0;

struct minister {
    int left_hand;
    int right_hand;
    int key_word;
//    int get_gold;
};

vector<minister> minister_each;

///*寻找最小序列的递归函数*/
//void find_min(){
//
//}

bool cmp(minister a, minister b) {
    return a.key_word < b.key_word;
}

/*大数乘法*/
int bigMul(int *temp_a_front, int a_front_szie, int left_hand) {
//    int result_size = a_front_szie + 1;
//    auto *result = new int[result_size];
    int give_a_number = a_front_szie;
    for (int i = 0; i < a_front_szie; i++) {
        temp_a_front[i] = temp_a_front[i] * left_hand;
    }

    int k = 0;
    while (true) {
        if(k >= give_a_number && temp_a_front[k+1] == 0){
            break;
        }
        if(temp_a_front[k] >= 10) {
            temp_a_front[k + 1] += temp_a_front[k] / 10;
            temp_a_front[k] = temp_a_front[k] % 10;
            if (k >= a_front_szie - 1) {
                give_a_number++;
            }
        }
        k++;
    }
//    give_a_number++;
//    return temp_a_front;
return give_a_number;
}

//大数除法 被除数数组 数组尺寸 除数
int *bigDivid(int *temp_a_front, int a_front_szie, int divisor) {
    int rem = 0;
    int *divide_result = new int[a_front_szie];
    for (int i = a_front_szie - 1; i >= 0; i--) {
        divide_result[i] = (rem * 10 + temp_a_front[i]) / divisor;
        rem = (rem * 10 + temp_a_front[i]) % divisor;
    }

    return divide_result;
}

int main() {
    /*大数乘法函数效果测试*/
//    int test[15] = {1,2,3,4,5};
//    bigMul(test,5,10);
//    for(int i=0; i<6;i++){
//        cout << test[i];
//    }

///*大数除法效果测试*/
//    int test[15] = {1,2,3,4,5};
//    auto *test_out = new int[15];
////    memset(test_out,0, sizeof(int)*15);
//    test_out = bigDivid(test,5,2);
//    for(int i=0; i<5;i++){
//        cout << test_out[i];
//    }

/*程序开始*/
    /*输入并排序主键*/
    minister temp_minister_each = {0, 0};
    cin >> minister_number;
    cin >> king_left >> king_right;
    for (int i = 0; i < minister_number; ++i) {
        cin >> temp_minister_each.left_hand >> temp_minister_each.right_hand;
        temp_minister_each.key_word = temp_minister_each.left_hand * temp_minister_each.right_hand; //排序主键
        minister_each.push_back(temp_minister_each);
    }
    sort(minister_each.begin(), minister_each.end(), cmp);

//    /*排序算法测试*/
//    for(auto it = minister_each.begin(); it != minister_each.end(); it++){
//        cout << (*it).left_hand <<' ' << (*it).right_hand << endl;
//    }


//    find_min();
///*找最大的数 需要用高精度*/
    int a_number = 1; //人的号数 第一个人是国王
    int temp_length = 0;
    int *temp_gold = new int[5005];

    /*初始化king_left*/
    int h = 0;
    a_front[0] = king_left; //最新所有left_hand相乘后的值
    while (a_front[h] >= 10) {
        a_front[h + 1] += a_front[h] / 10;
        a_front[h] = a_front[h] % 10;
        a_number++;
        h++;
    }


//    cout << a_number<

//
//
int count =0;
//
    for (vector<minister>::iterator it = minister_each.begin(); it != minister_each.end(); it++) {
//cout << count;
//count++;
        //
//
//    /*测试*/
////    for(int i=0; i
////        cout << a_front[i] << " " << endl;
////    }
//
        temp_gold = bigDivid(a_front, a_number, (*it).right_hand);//先除后乘
        temp_length = a_number;
        a_number = bigMul(a_front, a_number, (*it).left_hand);
//    while(a_front[a_number+1] != 0){
//        a_number++;
//    }
        //消除首位为零的影响
        while (temp_gold[temp_length - 1] == 0) {
            temp_length--;
        }

        //temp数字位数大于max
        if (temp_length > max_length) {
            max_length = temp_length;
//        for(int i=temp_length-1; i>=0; i--){
//            max_gold[i] = temp_gold[i];
//        }
            max_gold = temp_gold;
            //temp数字位数等于max 但大于max
        } else if (temp_length == max_length) {
            for (int i = temp_length - 1; i >= 0; i--) {
                if (max_gold[i] < temp_gold[i]) {
//                for(int k=temp_length-1; k>=0; k--){
//                    max_gold[k] = temp_gold[k];
//                }
                    max_gold = temp_gold;
                    break;
                }
            }
        }

//        free(temp_gold); //释放temp_gold
//delete[] temp_gold;
    }

//cout << temp_length << " temp_length";
//cout << max_length << endl;//调试

///*输出*/
//max_length = max_length - 1;
    for (int i = 0; i < max_length; i++) {
        cout << max_gold[max_length - 1 - i];
    }


//    cout << endl;
//    cout << "a_number" << a_number << endl;
//    for (int i = 0; i < a_number; i++) {
//        cout << a_front[i];
//    }

    return 0;
}

你可能感兴趣的:(洛谷 p1080国王游戏 题解)