蓝桥杯模拟赛 猜算式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/memories_sunset/article/details/65631561

第一次参加比赛,感觉之前学的好多东西都不知道怎么用,感觉准备很不充分。但是我还是想去拼一下,大不了明年接着刷,保研保本校跟着院长混。

废话不说,直接发出代码。暴力破解,其中代码块复制粘贴了很多,很容易的思路。通俗易懂。
由于我之前做过不少工程项目,我非常不习惯竞赛当中毫无意义的变量,这玩意遇到难题给你代码你都看不懂。非常爆炸。但也是我修为尚浅,继续努力吧。和CSDN诸君共勉。

#include <iostream>
using namespace std;
int numTimes[10];

void Initial()
{
    for (int i = 0; i < 10; i++)
    {
        numTimes[i] = 0;
    }
}

bool check()
{
    for (int i = 0; i < 10; i++)
    {
        if (numTimes[i] != 2)
        {
            return false;
        }
    }
    return true;
}


int main()
{
    Initial();
    for (int i = 100; i < 999; i++)
    {


        for (int j = 100; j < 999; j++)
        {
            int iGewei = i % 10;
            numTimes[iGewei]++;
            int iShiwei = (i / 10) % 10;
            numTimes[iShiwei]++;
            int iBaiwei = (i / 100);
            numTimes[iBaiwei]++;

            int jGewei = j % 10;
            numTimes[jGewei]++;
            int jShiwei = (j / 10) % 10;
            numTimes[jShiwei]++;
            int jBaiwei = (j / 100);
            numTimes[jBaiwei]++;

            int num1 = i*jGewei;
            if (num1 < 1000)
            {
                int num1Gewei = num1 % 10;
                numTimes[num1Gewei]++;
                int num1Shiwei = (num1 / 10) % 10;
                numTimes[num1Shiwei]++;
                int num1Baiwei = (num1 / 100);
                numTimes[num1Baiwei]++;
            }
            else
            {
                Initial();
                continue;
            }

            int num2 = i*jShiwei;
            if (num2 < 1000)
            {
                int num2Gewei = num2 % 10;
                numTimes[num2Gewei]++;
                int num2Shiwei = (num2 / 10) % 10;
                numTimes[num2Shiwei]++;
                int num2Baiwei = (num2 / 100);
                numTimes[num2Baiwei]++;
            }
            else
            {
                Initial();
                continue;
            }

            int num3 = i*jBaiwei;
            if (num3 < 1000)
            {
                int num3Gewei = num3 % 10;
                numTimes[num3Gewei]++;
                int num3Shiwei = (num3 / 10) % 10;
                numTimes[num3Shiwei]++;
                int num3Baiwei = (num3 / 100);
                numTimes[num3Baiwei]++;
            }
            else
            {
                Initial();
                continue;
            }

            int res = i*j;
            int resGewei = res % 10;
            numTimes[resGewei]++;
            int resShiwei = (res / 10) % 10;
            numTimes[resShiwei]++;
            int resBaiwei = (res / 100) % 10;
            numTimes[resBaiwei]++;
            int resQianwei = (res / 1000) % 10;
            numTimes[resQianwei]++;
            int resWanwei = (res / 10000) % 10;
            numTimes[resWanwei]++;

            if (check())
            {
                cout << res<<endl;
                system("pause");
                return 0;
            }
            else
            {
                Initial();
                continue;
            }

        }
    }
    cout << "Nothing" << endl;
    system("pause");
    return 0;

}

猜你喜欢

转载自blog.csdn.net/memories_sunset/article/details/65631561