Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

算法之枚举 #22

Open
jyzwf opened this issue Sep 17, 2017 · 0 comments
Open

算法之枚举 #22

jyzwf opened this issue Sep 17, 2017 · 0 comments

Comments

@jyzwf
Copy link
Owner

jyzwf commented Sep 17, 2017

枚举法就是从所有候选答案中搜索出正确的解,满足两个条件:

  1. 可预先确定候选答案的数量
  2. 候选答案的范围在求解之前必须要有一个确定的集合

demo

将数字带入下面式子,使等式成立:

image

code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a1,a2,a3,a4,a5;
    long multi,result;
    for(a1=1;a1<=9;a1++)
    {
           for(a2=0;a2<=9;a2++)
           {
                  for(a3=0;a3<=9;a3++)
                  {
                         for(a4=0;a4<=9;a4++)
                         {
                                for(a5=0;a5<=9;a5++){
                                   multi = a1*10000 + a2*1000 + a3*100 + a4*10 + a5;
                                   result = a5*100000 + a5*10000 + a5*1000 + a5*100 + a5*10 + a5;
                                   if(multi*a1 == result){
                                          printf("\n%5d%2d%2d%2d%2d\n",a1,a2,a3,a4,a5);
                                          printf("X%12d\n",a1);
                                          printf("_____________\n");
                                          printf("%3d%2d%2d%2d%2d%2d",a5,a5,a5,a5,a5,a5);
                                   }
                                }
                         }
                  }
           }
    }

    getch();
    return 0;
}

result

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant