-3

これはC++によって書かれたプログラム言語であり、方程式[] [] / [] = [] [] / [] = [] []/[]に繰り返しなしで1から9までのすべての数値を入力します。私はいくつかのテストを行いましたが、答えは完全ではないようです。たとえば、57/6 = 19/2 = 38/4、誰か助けてもらえますか?ありがとう。

4

1 に答える 1

1
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

/* [][]/[]=[][]/[]=[][]/[] */
bool equal(vector<double> a)
{
    double exc = 0.001;
    if (fabs((a[0]*10+a[1])/a[2] - (a[3]*10+a[4])/a[5]) > exc)
        return false;
    if (fabs((a[0]*10+a[1])/a[2] - (a[6]*10+a[7])/a[8]) > exc)
        return false;
    return true;
}
int main()
{
    vector<double> a;
    double data=1.0;
    for (int i=1; i<=9; ++i)
    {
        data = i*1.0;
        a.push_back(data);
    }
    while (next_permutation(a.begin(), a.end()))
    {
        if (equal(a))
        {
            for (vector<double>::iterator it=a.begin(); it!=a.end(); ++it)
                cout << *it << " ";
            cout << endl;
        }
    }
    return 0;
}

出力:

1 9 2 3 8 4 5 7 6
1 9 2 5 7 6 3 8 4
2 1 3 4 9 7 5 6 8
2 1 3 5 6 8 4 9 7
2 7 3 5 4 6 8 1 9
2 7 3 8 1 9 5 4 6
3 8 4 1 9 2 5 7 6
3 8 4 5 7 6 1 9 2
4 9 7 2 1 3 5 6 8
4 9 7 5 6 8 2 1 3
5 4 6 2 7 3 8 1 9
5 4 6 8 1 9 2 7 3
5 6 8 2 1 3 4 9 7
5 6 8 4 9 7 2 1 3
5 7 6 1 9 2 3 8 4
5 7 6 3 8 4 1 9 2
8 1 9 2 7 3 5 4 6
8 1 9 5 4 6 2 7 3

異なる順序でいくつかの回答があるかもしれません。必要に応じてフィルタリングできます。

于 2013-02-21T07:20:27.160 に答える