第七届蓝桥杯大赛个人赛省赛(软件类)B组

3.凑算式


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

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

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

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

这道有点坑吧,尤其对于我这种看题不仔细的,看到题是想着"/"这个运算是整数的,啪啪敲了代码一算两万多,想着也没毛病,可是答案说是29,找了半天看样例时发现了这里取商的值是小数......

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int vis[100];
int a[100], cnt;

void DFS(int t)
{
    if(t==9)
    {
        int f=1;
        for(int i=0; i<=8; i++)
        {
            if(a[i]==0)
            f=0;
        }
        if(f==1&&fabs((1.0*a[0]+1.0*a[1]/a[2]+1.0*(a[3]*100+a[4]*10+a[5])/(a[6]*100+a[7]*10+a[8]))-10)<1e-10)
        {
            cnt++;
        }
        return;
    }
    for(int i=1; i<=9; i++)
    {
        if(!vis[i])
        {
            a[t]=i;
            vis[i]=1;
            DFS(t+1);
            vis[i]=0;
            a[t]=0;
        }
    }
}

int main(int argc, char** argv) {
    cnt=0;
    for(int i=1; i<=9; i++)
    {
        memset(vis, 0, sizeof(vis));
        memset(a, 0, sizeof(a));
        vis[i]=1;
        a[0]=i;
        DFS(1);
    }
    printf("%d
", cnt);
    return 0;
}
原文地址:https://www.cnblogs.com/9968jie/p/6900197.html