蛮力法

来源:互联网 发布:哆点校园mac版 编辑:程序博客网 时间:2024/05/21 12:03

#include <stdio.h>
#include <stdlib.h>
//200624101101杨振平
//2008年11月4日21:43编写

//the algorithm of the force method
void main()
{
 //the defination of printing menu function
 int menu();
 //the defination of Algorithm of double method
    void dbAlgorithm();
 //the defination of Algorithm of int method
 void intAlgorithm();
 

 //inputs variable to choose menu's item
 int quit=0;
 while(!quit)
  switch(menu())
 {
  case 1: dbAlgorithm();break;
  case 2: intAlgorithm();break;
  default: quit=1;
 }

}
//print menu
int menu()
{
 printf("|----------------------------------------|/n");
 printf("|Please choose a menu:                   |/n");
 printf("|     1    :Algorithm of double method   |/n");
 printf("|     2    :Algorithm of int method      |/n");
 printf("|Other keys:exit...                      |/n");
 printf("|----------------------------------------|/n");
 int ch;
 scanf("%d",&ch);
 return ch;
}
//the implement of double method algorithm
void dbAlgorithm()
{
 double a,b,c,d,temp;
 for(a=0.01;a<7.11;a=a+0.01)
  for(b=0.01;b<7.11;b=b+0.01)
  {
   if(7.11-a-b>0)
   for(c=0.01;c<7.11;c=c+0.01)
   {
    d=7.11-a-b-c;
    if(d>0)
    {
     temp=a*b*c*d;
     if(temp>7.109999 && temp<7.110001)
     {
      printf("a=%f,/tb=%f,/tc=%f,/td=%f,/t/n",a,b,c,d);
      return ;
     }
    }
   }
  }
}
//the implement of int method algorithm
void intAlgorithm()
{
 int a,b,c,d,temp;
 for(a=1;a<711;a++)
  for(b=1;b<711;b++)
  {
   if(711-a-b>0)
   for(c=1;c<711;c++)
   {
    d=711-a-b-c;
    if(d>0)
    {
     temp=a*b*c*d;
     if(temp==711000000)
     {
      printf("a=%f,/tb=%f,/tc=%f,/td=%f,/t/n",(float)a/100,(float)b/100,(float)c/100,(float)d/100);
      return ;
     }
    }
   }
  }
}

原创粉丝点击