凑算式

本文探讨了一种特殊的数学问题——凑算式,并提供了几种不同的解题思路,包括暴力枚举法、改进后的枚举法及推荐的深度优先搜索算法。通过这些方法,我们能够找出所有可能的解。
凑算式


     B      DEF
A + --- + ------- = 10
     C      GHI
     
(如果显示有问题,可以参见【图1.jpg】)
 
 
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。


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


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


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

我自己一开始用的暴力枚举法写的,,,现在想想。。。那时候怕不是个傻子哦,而且我还翻出来了那时候的代码。不说了上图。

#include <stdio.h>
int main()
{
	int count=0;
	for(double a=1;a<10;a++)
	{
		for(double b=1;b<10;b++)
			{
				for(double c=1;c<10;c++)
					{
						for(double d=1;d<10;d++)
							{
								for(double e=1;e<10;e++)
									{
										for(double f=1;f<10;f++)
											{
												for(double g=1;g<10;g++)
													{
														for(double h=1;h<10;h++)
															{
																for(double i=1;i<10;i++)
																	{	double arr[9]={a,b,c,d,e,f,g,h,i};
																		double x=a+b/c+(d*100+e*10+f)/(g*100+h*10+i);
																		if(x==10.00&&a+b+c+d+e+f+g+h+i==45&&b/c+(d*100+e*10+f)/(g*100+h*10+i)==10-a)
																		{
																			for(int j=1;j<10;j++)
																			{
																				for(int k=j+1;k<9;k++)
																				{
																					if(arr[j]==arr[k])
																					break;
																					else if(j=9)
																					count++;
																				}
																			}
																			
																		}
																		else
																		continue;
																	}
	
															}
	
													}
	
											}
	
									}
	
							}
	
					}
	
			}
	
	}
	printf("%d\n",count);
	return 0;
}
当时我写那那么多。。。可以很明确的告诉你还写错了,所以我不推荐大家用枚举的方法,一个不小心全盘皆错,而且你还找不到问题在哪里,真的很白痴,如果你真的想用枚举,我这里附上别人写的枚举,仅供参考0.0
#include<iostream>  
#include<stdio.h>  
#include<stdlib.h>  
#include<string>  
#include<string.h>  
#include<math.h>  
#include<map>  
#include<vector>  
#include<algorithm>  
#include<queue>  
using namespace std;  
#define MAX 0x3f3f3f3f  
#define MIN -0x3f3f3f3f  
#define PI 3.14159265358979323  
#define N 1005  
int main()  
{  
    int a, b, c, d, e, f, g, h, i;  
    int sum = 0;  
    for (a = 1; a <= 9; a++)  
    {  
        for (b = 1; b <= 9; b++)  
        {  
            if (b == a)  
                continue;  
            for (c = 1; c <= 9; c++)  
            {  
                if (c == b || c == a)  
                    continue;  
                for (d = 1; d <= 9; d++)  
                {  
                    if (d == a || d == b || d == c)  
                        continue;  
                    for (e = 1; e <= 9; e++)  
                    {  
                        if (e == a || e == b || e == c || e == d)  
                            continue;  
                        for (f = 1; f <= 9; f++)  
                        {  
                            if (f == a || f == b || f == c || f == d || f == e)  
                                continue;  
                            for (g = 1; g <= 9; g++)  
                            {  
                                if (g == a || g == b || g == c || g == d || g == e || g == f)  
                                    continue;  
                                for (h = 1; h <= 9; h++)  
                                {  
                                    if (h == a || h == b || h == c || h == d || h == e || h == f || h == g)  
                                        continue;  
                                    for (i = 1; i <= 9; i++)  
                                    {  
                                        if (i == a || i == b || i == c || i == d || i == e || i == f || i == g || i == h)  
                                            continue;  
                                        else  
                                        {  
                                            if (fabs(a*1.0 + b*1.0 / c + (d * 100 + e * 10 + f)*1.0 / (g * 100 + h * 10 + i) - 10.00) < 0.0000000001)  
                                            {  
                                                sum++;  
                                                printf("%d%d%d%d%d%d%d%d%d\n", a, b, c, d, e, f, g, h, i);  
                                                printf("%.6lf\n", a*1.0 + b*1.0 / c + (d * 100 + e * 10 + f)*1.0 / (g * 100 + h * 10 + i));  
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
    printf("%d\n", sum);  
    return 0;  
}  
这里我个人推荐深度优先搜索的方法,当初我也找过别人写的深搜方法,,,但我看不懂,,,对很烦,看不懂。。。为什么看不懂,因为他们没写注释。。。后来我自学了一下,现在回想起这道题并不难,这里附上我自己写的,都写有很详细的注释,相信对你会有帮助的。
#include<iostream>
using namespace std;
double a[10]={0};	//涉及到除法,用双精度保存1-9这9个数 
int book[10]={0};//标记,book[i]=0代表book[i]的i这个数可用 
int cnt;
void dfs(int step)//step假设性盒子,9个数(1--9)假设放在9个盒子(0号盒子到8号盒子)里,和排列类似 
{
	//出口,当达到9个盒子全部放满之后,就可以结束这一轮了 
	if(step==9)
	{
		//这里是判定条件就好了 ,如果这9个数满足条件,计数cnt加1 
		if(a[0]+a[1]/a[2]+(a[3]*100+a[4]*10+a[5])/(a[6]*100+a[7]*10+a[8])==10)
		{
			cnt++;	
		}

		return;
	}
	
	for(int i=1;i<10;i++)
	{
		//判断这个数是否可用
		if(book[i]==0)
		{
			a[step]=i;
			book[i]=1;//1代表这个i在接下来的放置中不会再使用
			dfs(step+1);
			book[i]=0;//一轮下来回收这个数字,下一轮中再用 
		}
	}
	return;
}
int main()
{
	dfs(0);//从0号盒子开始放 
	cout<<cnt<<endl; 
	return 0;
}
如果还不懂,不要急。。。随着你的深入学习,终有一天你会懂得,不要急。。。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值