如何编程所有可能的答案[][]/[]=[][]/[[]=[]]/[],从1到9没有重复数字

How to program all the possible for answer [][]/[] = [][]/[] = [][]/[] with no repeat number from 1 to 9?

本文关键字:数字 有可能 答案 何编程 编程      更新时间:2023-10-16

这是一个由C++编写的程序语言,在公式[][]/[]=[][]/[[]=[]][]/[]中填充从1到9的所有数字,不重复。我做了一些测试,但答案似乎并不完全,例如57/6=19/2=38/4,有人能帮忙吗?谢谢

#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

也许有几个不同顺序的答案,你可以根据需要进行筛选。