当前位置:网站首页>结果填空 凑算式(DFS*C语言)
结果填空 凑算式(DFS*C语言)
2022-07-28 05:19:00 【小白鼠零号】
如图,这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。

比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
答案
29
解题过程
用A[9]存储1~9,用B[i]表示A~I(下标i从0~8分别代表A~I的值),用dfs每次更替B[i]中的值,在通过is_true()判断算式是否成立。
附上代码
#include<iostream>
using namespace std;
int a[9]={1,2,3,4,5,6,7,8,9};//代表A~I
int b[9]={0};//存储每次A~I所代表的值,其中
int vis[9]={0};//0-没访问
int counts=0;
int main()
{
void dfs(int);
dfs(0);
cout<<counts<<endl;
return 0;
}
bool is_true()
{
int GHI=b[6]*100+b[7]*10+b[8];
int DEF=b[3]*100+b[4]*10+b[5];
if(b[0]*b[2]*GHI+b[1]*GHI+b[2]*DEF==b[2]*GHI*10)
return true;
return false;
}
void dfs(int index)
{
if(index==9)
{
if(is_true())
counts++;
return;
}
for(int i=0;i<9;i++)
{
if(vis[i]==0)
{
vis[i]=1;
b[index]=a[i];
dfs(index+1);
vis[i]=0;
}
}
}
边栏推荐
猜你喜欢

SVG了解与绘图应用

You must configure either the server or JDBC driver (via the ‘serverTimezone)

顺序表oj之删除特定的元素

【uni-app】uni-app中scroll-into-view的使用

冶金物理化学复习 --- 复杂反应的速率方程

Review of metallurgical physical chemistry -- Fundamentals of metallurgical reaction kinetics and characteristics of multiphase reaction kinetics

对极大似然估计、梯度下降、线性回归、逻辑回归的理解

链表中关于快慢指针的oj题

A file upload tool website written by individuals

Advanced multithreading: the role and implementation principle of volatile
随机推荐
Idea uses dev tool to realize hot deployment
Pytorch uses hook to get feature map
Docker deploy mysql5.7.35
异步编程Promise
A file upload tool website written by individuals
Mutual conversion between latex and word
Openjudge: perpetual calendar
分支与循环语句
对象内多个数组的对应下标相加
Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later
Thesis writing function words
The difference between get and post
GD32F407 移植FreeRTOS+Lwip
ArcGIS Engine开发资源
Review of metallurgical physical chemistry -- cathodic polarization, overpotential, anode and anode process in metal electrodeposition
JVM notes 3: class loading and bytecode Technology
树莓派串口
Centos7 install MySQL 5.7
标准C语言学习总结9
JS字符串方法大全