凑算式

本文探讨了一种特定的数学方程求解问题,通过不同数字的排列组合来寻找所有可能的解。文中提供了三种不同的算法实现思路:深度优先搜索、暴力求解及全排列的方法,并给出了具体的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。

比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?

注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。

A-I代表1-9的数字,不是0-9

#include <iostream>
using namespace std;
bool vis[10];
double a[10];
int ans=0;
void dfs(int step);
int main(){
    dfs(1);
    cout<<ans<<endl;
    return 0;
}
void dfs(int step){
     if(step==10)     
    {  
        if((a[1]+a[2]/a[3]+(a[4]*100+a[5]*10+a[6])/(a[7]*100+a[8]*10+a[9]))==10)  //判断是否满足等式  
        {  
            ans++;  //如果满足,可行解+1
        }  
        return;  
    }  
    for(double i = 1; i < 10; i++)  
    {  
        if(vis[(int)i]==0)  
        {  
            a[step]=i;  
            vis[(int)i]=1;  
            dfs(step+1);  
            vis[(int)i]=0;  
        }  
    }  
    return; 
}

暴力解决:

#include <iostream>
using namespace std;
bool judge(float* a){
    for (int i = 1; i < 10; i++)
    {
        for (int j = 1; j < 10; j++)
        {
            if (a[i]==a[j] && i!=j)
            {
                return 0;
            }
        }
    }
    return 1;
}
int main(){
    float a[11];
    int ans=0;
    for (a[1] = 1; a[1] < 10; a[1]++)
    {
        for (a[2] = 1; a[2] < 10; a[2]++)
        {
            for (a[3] = 1; a[3] < 10; a[3]++)
            {
                for (a[4] = 1; a[4] < 10; a[4]++)
                {
                    for (a[5] = 1; a[5] < 10; a[5]++)
                    {
                        for (a[6] = 1; a[6] < 10; a[6]++)
                        {
                            for (a[7] = 1; a[7] < 10; a[7]++)
                            {
                                for (a[8] = 1; a[8] < 10; a[8]++)
                                {
                                    for (a[9] = 1; a[9] < 10; a[9]++)
                                    {
                                        if (judge(a))
                                        {
                                            if ((a[1]+a[2]/a[3]+(a[4]*100+a[5]*10+a[6])/(a[7]*100+a[8]*10+a[9]))==10)
                                            {
                                                ans++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

全排列:

#include <iostream>
using namespace std;
int ans=0;
double arr[]={1,2,3,4,5,6,7,8,9};

void swap(double &a,double &b){
    int temp=a;
    a=b;
    b=temp;
}
void perm(double a[],int low,int high){
    if(low == high){   //当low==high时,此时list就是其中一个排列,输出list
        if(a[0] + a[1]/a[2] + (a[3]*100+a[4]*10+a[5])/(a[6]*100+a[7]*10+a[8]) == 10 )
            ans++; 
    }else{
        for(int i=low;i<=high;i++){//每个元素与第一个元素交换
            swap(a[i],a[low]); 
            perm(a,low+1,high); //交换后,得到子序列,用函数perm得到子序列的全排列
            swap(a[i],a[low]);//最后,将元素交换回来,复原,然后交换另一个元素
        }
    }
}

int main(){

    perm(arr,0,8);
    cout << ans;
    return 0;
}

### 使用深度优先搜索 (DFS) 解决算式问题 在 C++ 中,可以利用深度优先搜索 (DFS) 来解决算式问题。这类问题通常涉及通过加减乘除运算符组合给定的一组数字,使其达到目标值。 以下是基于 DFS 的解决方案: #### 代码实现 ```cpp #include <iostream> #include <vector> using namespace std; // 定义全局变量存储结果 vector<string> results; int target; // 辅助函数用于执行 DFS 搜索 void dfs(vector<int>& nums, string path, long currentSum, long prevNum, int index) { if (index == nums.size()) { if (currentSum == target) { results.push_back(path); } return; } // 构造当前数字字符串 string numStr = ""; long currNum = 0; for (int i = index; i < nums.size(); ++i) { numStr += to_string(nums[i]); currNum = currNum * 10 + nums[i]; // 防止出现前导零的情况 if (numStr.length() > 1 && numStr[0] == '0') break; // 如果是第一个数字,则无需添加操作符 if (index == 0) { dfs(nums, numStr, currNum, currNum, i + 1); } else { // 尝试 "+" 运算符 dfs(nums, path + "+" + numStr, currentSum + currNum, currNum, i + 1); // 尝试 "-" 运算符 dfs(nums, path + "-" + numStr, currentSum - currNum, -currNum, i + 1); // 尝试 "*" 运算符(注意乘法的优先级) dfs(nums, path + "*" + numStr, currentSum - prevNum + prevNum * currNum, prevNum * currNum, i + 1); } } } // 主函数入口 vector<string> solveEquation(string digits, int goal) { vector<int> nums; for (char c : digits) { nums.push_back(c - '0'); } target = goal; dfs(nums, "", 0, 0, 0); return results; } int main() { string inputDigits = "123"; // 输入的数字串 int targetValue = 6; // 目标值 vector<string> result = solveEquation(inputDigits, targetValue); cout << "Possible equations:" << endl; for (string eq : result) { cout << eq << "=" << targetValue << endl; } return 0; } ``` --- #### 代码说明 1. **输入参数** 函数 `solveEquation` 接收两个参数:一个由字符组成的数字串和目标值。 2. **核心逻辑** 利用递归调用 `dfs` 方法尝试每种可能的操作符 (`+`, `-`, `*`) 和分割方式,逐步构建表达式并计算其值。 3. **剪枝条件** - 当遇到非法情况(如超出范围或无法满足目标值)时提前返回。 - 处理多位数时防止前导零[^1]。 4. **乘法优先级处理** 在处理乘法时,需调整之前的总和以反映正确的优先级。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值