如何编程所有可能的答案[][]/[]=[][]/[[]=[]]/[],从1到9没有重复数字
How to program all the possible for answer [][]/[] = [][]/[] = [][]/[] with no repeat number from 1 to 9?
这是一个由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
也许有几个不同顺序的答案,你可以根据需要进行筛选。
相关文章:
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 有可能使shared_ptr协变吗
- 有可能在信号处理程序中设置promise吗
- 是否有可能实现O(N)时间和O(1)空间解决方案,以实现C++中的字符串循环移位
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- 是否有可能使用debug_info获取ELF文件的源代码?
- C++,是否有可能/如何定义在.h和.cpp源文件中调用函数的类构造函数
- 有可能在C++中有类的查找表吗
- 是否有可能让 c++ dll 在后台运行 python 程序并让它填充向量图?如果是这样,如何?
- 向量的大小是否有可能为 1 但其中的元素数量为零?
- 是否有可能编写新的叮当声现代化规则?
- 是否有可能通过指向另一个未关联的子对象的指针来获取指向一个子对象的指针?
- 是否有可能通过演绎指南实现整个 std::make_tuple 功能?
- 是否有可能在没有复制的情况下传递 std::vector<int> 作为参数来获得 std::vector<std::array<int, 3>>?
- 是否有可能具有放入容器的移动操作的类型?
- 是否有可能通过溢出 C 中的第一个元素来写入数组第二个元素
- 声明是否有可能逃脱其封闭的名称空间
- C++ 浮点小数位,数字末尾可能突然有"05"
- 是否有可能在编译时计算数字的阶乘,但不使用枚举?