转跳到内容

神之天平自动计算配平工具(可在线使用)


推荐贴

神之天平太好玩了!

因为我想体验自己选择道具去摆天平的乐趣,但是又懒得手动去算配平摆放方式,所以写了段程序。

很多版友可能没有编译条件,没关系,可用如下网站在线编译和运行:

https://www.onlinegdb.com/online_c++_compiler

代码:

剧透
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
#include <iostream>
#include <stack>

using namespace std;
//下一行,9替换为你拥有的天平盘子总数
const int WTCT = 9;
//下一行,5替换为你拥有的左边盘子数量
const int leftMax = 5;
//下一行,4替换为你拥有的右边盘子数量
const int rightMax = 4;
stack <int> ltwt;
stack <int> rtwt;
//下一行,每个数字即代表一个道具的业。数字的数量要等于拥有的盘子总数,比如有9个盘子,那么就填入9个数字,用半角逗号分隔
int wt[WTCT] = { 50, 63, 57, 29, 85, 41, 65, 90, 70 };
stack<int> wts;


bool balance(int left, int right){
    if (wts.size() == 0)
    //下一行,代表可以接受的配平目标,目前是完美平衡。如果找不到完美平衡方案,可以把下一行注释掉(即行首加上//),然后把再下一行的注释消除(行首//去掉),其中两个1代表可接受左右相差为1的方案,可替换为其他数字
        return left == right;
        //return left - right <= 1 && right - left <= 1;
    int top = wts.top();
    wts.pop();
    if (ltwt.size() < leftMax)
    {
        ltwt.push(top);
        if (balance(left + top, right)) return true;
        ltwt.pop();
    }
    if (rtwt.size() < rightMax)
    {
        rtwt.push(top);
        if (balance(left, right + top)) return true;
        rtwt.pop();
    }
    wts.push(top);
    return false;
}

int main()
{
    for (int i = 0;i < WTCT;i++){
        wts.push(wt[i]);
    }
    if (balance(0, 0))
    {
        for (int i = 0;i < leftMax;i++)
        {
            cout << ltwt.top() << ',';
            ltwt.pop();
        }
        cout << endl;
        for (int i = 0;i < rightMax;i++)
        {
            cout << rtwt.top() << ',';
            rtwt.pop();
        }
    }
    else
        cout << "Not found!";
}

使用方法:

1. 在游戏中选取自己想要放上天平的道具,记下每个道具的业

2. 打开我给的网址,把页面里的代码替换为我给的代码,并且按照提示改数字。需要改的总共有4行,第五处可以先无视。

3. 点击绿色的Run按钮,代码会编译和运行。成功配平的话下方会输出两行数字,表示天平左右各自要摆放的道具。否则会输出Not found! 代表无法配平。如果输出其他东西,可能是你改了不该改的地方,需要从第2步重来。

4. 无法完美配平时也可以退而求其次,找不完美的方案,代码里有说明(即第2步中无视的地方)。

,由MikeYi修改
注释
女武松 女武松 50.00节操
链接到点评
  • MikeYi将标题更改为神之天平自动计算配平工具(可在线使用)
游客
此主题已关闭。
×
×
  • 新建...

重要消息

为使您更好地使用该站点,请仔细阅读以下内容: 使用条款