对于整数n(0<=n<=255),能否找到一个公式n=a*b+c*d+e*f,其中0<=a<=3,0<=c<=7,0<=e<=3,d.b.f为整数。
如何通过程序找出满足以上公式的b.d.f

解决方案 »

  1.   

    太难了!
    a,b,c 确定还好说。
      

  2.   

    too easy!
    直接用穷举不就得了!!
    [email protected]
      

  3.   

    a,c,e是整数吗?
    要不然解就太多了
      

  4.   

    a,b,c,d,e,f是正整数.我也不知道是否该公式确实存在。我现在是需要验证该公式是否确实存在。如果存在找出满足以上公式的b.d.f
      

  5.   

    如果a,b,c,d,e,f都为整数的话
    最苯的办法就是循环了。
    int n=100;
    int a,b,c,d,e,f;

    //n=a*b+c*d+e*f;
    for(a=0;a<=3;a++)
    {
    for(c=0;c<=7;c++)
    {
    for(e=0;e<=3;e++)
    {
    for(b=0;b<=n;b++)
    {
    for(d=0;d<=n;d++)
    {
    for(f=0;f<=n;f++)
    {
    if(a*b+c*d+e*f==n)
    {
    printf("a=%d,b=%d,c=%d,d=%d,e=%d,f=%d\n",a,b,c,d,e,f);
    }
    }
    }
    }
    }
    }
    }

    如果是float的话,看楼下的高人们有什么办法。
    gz
      

  6.   

    什么意思? a, c,  e是给定的吗?
      

  7.   

    a, c,  e在指定范围内可变。b d f是固定整数.
    我希望n=a*45+b*12+c*3(仅仅是假设)
      

  8.   

    // cal.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include "iostream.h"int main(int argc, char* argv[])
    {
      int a[]={0,1,2,3};
      int c[]={0,1,2,3,4,5,6,7};
      int e[]={0,1,2,3};
      int b,d,f;
      int i,j,k;
      int n;
      int temp;
    for(n=0;n<=255;n++)
    {
      for(i=0;i<4;i++)
    for(j=0;j<8;j++)
      for(k=0;k<4;k++)
      {
    for(b=1;b<=255;b++)
      for(d=1;d<=255;d++)
    for(f=1;f<=255;f++)
    {
      temp=a[i]*b+c[j]*d+e[k]*f;
      if(temp==n)
    cout<<"n=  "<<n<<"  :"<<a[i]<<","<<c[j]<<","<<e[k]<<"  :  "<<b<<","<<d<<","<<f<<endl;
    }
      }}
    return 0;
    }
      

  9.   

    下面是n=100时的部分输出结果:
    n=  100  :2,1,1  :  26,2,46
    n=  100  :2,1,1  :  26,3,45
    n=  100  :2,1,1  :  26,4,44
    n=  100  :2,1,1  :  26,5,43
    n=  100  :2,1,1  :  26,6,42
    n=  100  :2,1,1  :  26,7,41
    n=  100  :2,1,1  :  26,8,40
    n=  100  :2,1,1  :  26,9,39
    n=  100  :2,1,1  :  26,10,38
    n=  100  :2,1,1  :  26,11,37
    n=  100  :2,1,1  :  26,12,36
    n=  100  :2,1,1  :  26,13,35
    n=  100  :2,1,1  :  26,14,34
    n=  100  :2,1,1  :  26,15,33
    n=  100  :2,1,1  :  26,16,32
    n=  100  :2,1,1  :  26,17,31
    n=  100  :2,1,1  :  26,18,30
    n=  100  :2,1,1  :  26,19,29
    n=  100  :2,1,1  :  26,20,28
    n=  100  :2,1,1  :  26,21,27
    n=  100  :2,1,1  :  26,22,26
    n=  100  :2,1,1  :  26,23,25
    n=  100  :2,1,1  :  26,24,24
      

  10.   

    我觉得大家错误理解我的意思了。不论n等于多少,b d f是固定整数。a, c,  e在指定范围内可变。而且该公式对所有n均适用.也就是说n=100和n=200时的b d f是一样的
      

  11.   

    我估计不存在这样的公式,理由如下:
    假设存在公式n=a*b+c*d+e*f,其中b.d.f为定值。
    所以对于整数n+k会有:n+k=a1*b+c1*d+e1*f=a*b+c*d+e*f+k=>
                         (a-a1)*b+(c-c1)*d+(e-e1)*f+k=0
    由于b,d,f为定值,而(a-a1),(c-c1),(e-e1),k可以变化,有线性代数知识可知,这可看成一个线性方程组,未知数是:b,d,f.由于方程的个数肯定会大于3,所以方程没有解:即找不到b,d,f满足公式要求。
    所以,各大虾,别找了,肯定找不到!
      

  12.   

    b,d,f,三个数为常数,a,c,e又都在一定范围内,那么
    n=a*b+c*d+e*f一定存在上限和下限,那么如果一个数m>n的上限,或者m<n的下限,那么m=n=a*b+c*d+e*f就不存在,所以要想让所有的n都适用这个公式是不现实的。
      

  13.   

    #include "iostream.h"void start();
    void getbdf();
    bool verify();int ace[3];
    int bdf[3];
    int first = 191;void main()
    {
    start();
    }void start()
    {
    for( int a=0; a<=3; a++ )
    {
    for( int c=0; c<=7; c++ )
    {
    for( int e=0; e<=3; e++ )
    {
    ace[0] = a;
    ace[1] = c;
    ace[2] = e;
    getbdf();
    }
    }
    }
    }void getbdf()
    {
    for( int b=0; b<=first; b++ )
    {
    for( int d=0; d<=first-b*ace[0]; d++ )
    {
    for( int f=0; f<= first-b*ace[0]-d*ace[1]; f++ )
    {
    if( b*ace[0]+d*ace[1]+f*ace[2] == first )
    {
    bdf[0] = b;
    bdf[1] = d;
    bdf[2] = f;
    if( verify() )
    cout << bdf[0] << bdf[1] << bdf[2] << endl;
    }
    }
    }
    }

    }bool verify()
    {
    int ok;
    for( int n = 0; n <= 255; n++ )
    {
    ok = false;
    for( int a = 0; a <= 3; a++ )
    {
    for( int c = 0; c <= 7; c++ )
    {
    for( int e = 0; e <= 3; e++ )
    {
    if( a*bdf[0]+c*bdf[1]+e*bdf[2] == n  )
    {
    ok = true;
    break;
    }
    }
    if( ok )
    break;
    }
    if( ok )
    break;
    }
    if( !ok )
    return false;
    }
    if( n > 255 )
    return true;
    }