奥数加法

这篇博客探讨了一种算法问题,即如何从9个不同的3位数中选取3个,使得前两个3位数相加的和等于第三个3位数,并且在加法过程中每个数字都不能重复使用。
摘要由CSDN通过智能技术生成

算法从9个数中取出3个3位数其中两个3位数相加等于另一个3位数其中每位数字不能重复,即

__ __ __ + __ __ __ = __ __ __

 

/*
1.利用深搜思想
2.将9个位置看成9个盒子,分别放入不重复的数字,进而遍历所有可能性

*/
#include<iostream>
using namespace std;

int book[10] = {0};
int step = 1;
int str[10];

int total = 0;
int flag ;
void dfs(int step)
{
	if(step == 10)
	{
		if((str[7]*100+str[8]*10+str[9]) == 
                    (str[1]*100+str[2]*10+str[3]) + (str[4]*100+str[5]*10+str[6]))
		{
			total ++;
			if(flag == 8)  //这里只是为了调整输出格式,不影响代码
			{
				cout << endl;
				flag = 0;
			}
			cout << str[1]<<str[2]<<str[3] << '+' << 
                            str[4]<<str[5]<<str[6] << '=' << 
                            str[7] << str[8] << str[9]<<'\t';
			flag ++;
			return ;
		}
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值