最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

万能的搜索

运维笔记admin2浏览0评论
#include<iostream>
#include<stdio.h>
using namespace std;
int a[10], book[10], total=0;

void dfs(int step)
{
    int i;
    if(step == 10){
        //判断是否满足等式
        if(a[1]*100+a[2]*10+a[3] + a[4]*100+a[5]*10+a[6] == a[7]*100+a[8]*10+a[9]){
            total++;
            cout<<a[1]<<a[2]<<a[3]<<'+'<<a[4]<<a[5]<<a[6]<<'='<<a[7]<<a[8]<<a[9]<<endl;
        }
        return;
    }
///此时站在第step个盒子面前,应该放哪张牌呢?
///按照1、2、3……n的顺序一一尝试
    for(i=1; i<=9; i++){
        if(book[i] == 0){
            a[step] = i;
            book[i] = 1;

            dfs(step+1);
            book[i] = 0;
        }
    }
    return;
}

int main()
{
    dfs(1);

    cout<<"total="<<total/2;

    getchar();getchar();
    return 0;
}
仍然来自于《阿哈算法》
发布评论

评论列表(0)

  1. 暂无评论