• DFS小题


    原创


    题目为:()()()+()()()=()()()

    将1~9这9个数字填入括号,每个数字只能用一次。

    枚举:

     1 public class Test {
     2     public static void main(String[] args){
     3         int a[]=new int[9];
     4         int flag[]=new int[10];
     5         long total=0L;
     6         for(int i=1;i<=9;i++) {
     7             flag[i]=0;
     8         }
     9         for(a[0]=1;a[0]<=9;a[0]++) {
    10             for(a[1]=1;a[1]<=9;a[1]++) {
    11                 for(a[2]=1;a[2]<=9;a[2]++) {
    12                     for(a[3]=1;a[3]<=9;a[3]++) {
    13                         for(a[4]=1;a[4]<=9;a[4]++) {
    14                             for(a[5]=1;a[5]<=9;a[5]++) {
    15                                 for(a[6]=1;a[6]<=9;a[6]++) {
    16                                     for(a[7]=1;a[7]<=9;a[7]++) {
    17                                         for(a[8]=1;a[8]<=9;a[8]++) {
    18                                             int tt=0;
    19                                             for(int i=0;i<=8;i++) {
    20                                                 flag[a[i]]=1;
    21                                             }
    22                                             for(int i=1;i<=9;i++) {
    23                                                 tt+=flag[i];
    24                                             }
    25                                             if(tt==9) {
    26                                                 if(a[0]*100+a[1]*10+a[2]+a[3]*100+a[4]*10+a[5]==a[6]*100+a[7]*10+a[8]) {
    27                                                     total++;
    28                                                     tt=0;
    29                                                 }
    30                                             }
    31                                             for(int i=1;i<=9;i++) {    //数组恢复
    32                                                 flag[i]=0;
    33                                             }
    34                                             
    35                                         }
    36                                     }
    37                                 }
    38                             }
    39                         }
    40                     }
    41                 }
    42             }
    43         }
    44         System.out.println(total/2);
    45     }
    46 }
    View Code

    全排列:

    关于全排列,请看我上一篇博客:https://www.cnblogs.com/chiweiming/p/9279858.html

     1 public class Test{
     2     
     3     static int flag[]=new int[10];    //flag[i]=0代表第i个数未用
     4     static int win[]=new int[9];
     5     static long total=0L;
     6     
     7     public static void dfs(int step) {    //第step个格子
     8         if(step==9) {
     9             if(win[0]*100+win[1]*10+win[2]+win[3]*100+win[4]*10+win[5]==win[6]*100+win[7]*10+win[8]) {
    10                 total++;
    11             }
    12             return;
    13         }
    14         for(int i=1;i<=9;i++) {    //尝试按顺序将1~9其中一个放入格子
    15             if(flag[i]==0) {
    16                 win[step]=i;
    17                 flag[i]=1;
    18                 dfs(step+1);
    19                 flag[i]=0;
    20             }
    21         }
    22     }
    23     
    24     public static void main(String args[]) {
    25         for(int i=1;i<=9;i++) {
    26             flag[i]=0;
    27         }
    28         dfs(0);
    29         System.out.println(total/2);
    30     }
    31 }
    View Code

    答案:168

    23:26:20

    2018-07-09

  • 相关阅读:
    JS语言中的JSON.parse()和JSON.stringify()
    Django中 @login_required用法简介
    Django model中的save后的return
    windows下gethostbyname 调用失败
    学习打造自己的DEBUG_NEW
    关于new/delete、malloc/free的内存泄漏检测
    C++连接mysql的两种方式(ADO连接和mysql api连接)
    windows下Redis编译安装
    mysql之字符编码问题
    mysql错误用法insert into where
  • 原文地址:https://www.cnblogs.com/chiweiming/p/9283331.html
Copyright © 2020-2023  润新知