您的位置:程序门 -> .net技术 -> c#



一个算法,给个思路


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


一个算法,给个思路[已结贴,结贴人:asom]
发表于:2007-01-20 13:22:32 楼主
有1至9,9个不同的数字,组成三个三位数,要求第二个三位数是第一个三位数的两倍,第三个三位数是第一个三位数的三倍,请列出三个三位数是多少?
请哪位大是给个思路,列一下这个算法,想了办半天算不出来啊
发表于:2007-01-20 13:23:321楼 得分:0
自己做个沙发
发表于:2007-01-20 13:38:302楼 得分:0
那就自己等吧
发表于:2007-01-20 13:46:163楼 得分:0
不记得说9个数字不能重复
发表于:2007-01-20 13:52:174楼 得分:0
沒有答案的
发表于:2007-01-20 13:57:465楼 得分:0
我用手工算出来了192     384       576
但我不知道列这个程序出来算啊
发表于:2007-01-20 13:58:516楼 得分:0
回溯
发表于:2007-01-20 14:09:177楼 得分:4
从算法角度来说,
线性代数...
矩阵方程...
设第一个数是x*100+y*10+z
第二个的各位就是(2*z> 10?2*z-10:2*z)
类推...

当然,更偷懒的方法就是一个循环
第一个数只能是102到325之间,做一个for,也能求出...
发表于:2007-01-20 14:10:448楼 得分:0
up,一下吧@没有思路
111,222,333这样的算不
发表于:2007-01-20 17:59:479楼 得分:0
穷举是肯定可以的
发表于:2007-01-20 18:21:2610楼 得分:5
176   352   528
178   356   534
182   364   546
192   384   576
194   388   582
218   436   654
219   438   657
238   476   714
239   478   717
273   546   819
327   654   981
发表于:2007-01-20 18:23:4511楼 得分:0
136   272   408
169   338   507
267   534   801
269   538   807

这几个里面含有数字0,不符合“1至9,9个不同数字的要求”
发表于:2007-01-20 18:27:0612楼 得分:0
for   (int   i   =   1;   i   <=   3;   ++i)   //   400的三倍超过999了,肯定不行
{
for   (int   j   =   1;   j   <=   9;   ++j)
{
if   (j   ==   i)   continue;
for   (int   k   =   1;   k   <=   9;   ++k)
{
if   (k   ==   i   ¦ ¦   k   ==   j)   continue;
int   n1   =   i*100   +   j*10   +   k;
int   n2   =   2   *   n1;
int   n3   =   3   *   n1;
int   t;
t   =   n2   %   10;   //   个位数
if   (t   ==   i   ¦ ¦   t   ==   j   ¦ ¦   t   ==   k)   continue;
t   =   n2   %   100   /   10;   //   十位数
if   (t   ==   i   ¦ ¦   t   ==   j   ¦ ¦   t   ==   k)   continue;
t   =   n2   /   100;   //   百位数
if   (t   ==   i   ¦ ¦   t   ==   j   ¦ ¦   t   ==   k)   continue;

if   (n3   >   999)   continue;
t   =   n3   %   10;
if   (t   ==   i   ¦ ¦   t   ==   j   ¦ ¦   t   ==   k)   continue;
t   =   n3   %   100   /   10;
if   (t   ==   i   ¦ ¦   t   ==   j   ¦ ¦   t   ==   k)   continue;
t   =   n3   /   100;
if   (t   ==   i   ¦ ¦   t   ==   j   ¦ ¦   t   ==   k)   continue;
console.writeline( "{0}   {1}   {2} ",   n1,   n2,   n3);
}
}
}
发表于:2007-01-20 20:51:5913楼 得分:8
int   x,y,z;
int   a,b,c;

for(x=1;x <4;x++)
{
for(y=1;y <10;y++)
{
if(x==y)   continue;
for(z=1;z <10;z++)
{
if(z==x ¦ ¦z==y)   continue;

a   =   x*100+y*10+z;
b   =   2*a;
c   =   3*a;
int   i   =   b/100; //百位数字
if(i==x ¦ ¦i==y ¦ ¦i==z)   continue;

int   j   =   b%100/10; //十位数字
if(j==x ¦ ¦j==y ¦ ¦j==z)   continue;     //与   a   的所以数字比较
if(j==i ¦ ¦j==0)   continue; //比较本身的数字

int   k   =   b%100%10; //个位数字
if(k==x ¦ ¦k==y ¦ ¦k==z)   continue;     //与   a   的所有数字   比较
if(k==i ¦ ¦k==j ¦ ¦k==0)   continue;     //比较本身的数字

int   t   =   c/100;
if(t==x ¦ ¦t==y ¦ ¦t==z ¦ ¦t==i ¦ ¦t==j ¦ ¦t==k)   continue;

int   m   =   c%100/10;
if(m==x ¦ ¦m==y ¦ ¦m==z ¦ ¦m==i ¦ ¦m==j ¦ ¦m==k)   continue;
if(m==t ¦ ¦m==0)   continue;

int   n   =   c%100%10;
if(n==x ¦ ¦n==y ¦ ¦n==z ¦ ¦n==i ¦ ¦n==j ¦ ¦n==k)   continue;
if(n==t ¦ ¦n==m ¦ ¦n==0)   continue;

if(c> 987)   continue;

this.richtextbox1.text   +=   a.tostring()+ "     "+b.tostring()+ "     "+c.tostring()+ "\n ";
}
}
}
发表于:2007-01-20 20:53:1914楼 得分:0
执行结果
192     384     576
219     438     657
273     546     819
327     654     981
发表于:2007-01-20 21:10:1215楼 得分:0
晕了   -_-!   第一次写的代码错了
改了一下,得到结果
192     384     576
219     438     657
273     546     819
327     654     981


for   (int   i   =   1;   i   <=   3;   ++i)
{
for   (int   j   =   1;   j   <=   9;   ++j)
{
if   (j   ==   i)   continue;
for   (int   k   =   1;   k   <=   9;   ++k)
{
if   (k   ==   i   ¦ ¦   k   ==   j)   continue;
int   n1   =   i*100   +   j*10   +   k;
int   n2   =   2   *   n1;
int   n3   =   3   *   n1;
int   a   =   n2   %   10;
if   (a   ==   i   ¦ ¦   a   ==   j   ¦ ¦   a   ==   k   ¦ ¦   a   ==   0)   continue;
int   b   =   n2   %   100   /   10;
if   (b   ==   i   ¦ ¦   b   ==   j   ¦ ¦   b   ==   k   ¦ ¦   b   ==   a   ¦ ¦   b   ==   0)   continue;
int   c   =   n2   /   100;
if   (c   ==   i   ¦ ¦   c   ==   j   ¦ ¦   c   ==   k   ¦ ¦   c   ==   a   ¦ ¦   c   ==   b   ¦ ¦   c   ==   0)   continue;

if   (n3   >   999)   continue;
int   d   =   n3   %   10;
if   (d   ==   i   ¦ ¦   d   ==   j   ¦ ¦   d   ==   k   ¦ ¦   d   ==   a   ¦ ¦   d   ==   b   ¦ ¦   d   ==   c   ¦ ¦   d   ==   0)   continue;
int   e   =   n3   %   100   /   10;
if   (e   ==   i   ¦ ¦   e   ==   j   ¦ ¦   e   ==   k   ¦ ¦   e   ==   a   ¦ ¦   e   ==   b   ¦ ¦   e   ==   c   ¦ ¦   e   ==   d   ¦ ¦   e   ==   0)   continue;
int   f   =   n3   /   100;
if   (f   ==   i   ¦ ¦   f   ==   j   ¦ ¦   f   ==   k   ¦ ¦   f   ==   a   ¦ ¦   f   ==   b   ¦ ¦   f   ==   c   ¦ ¦   f   ==   d   ¦ ¦   f   ==   e   ¦ ¦   f   ==   0)   continue;
console.writeline( "{0}   {1}   {2} ",   n1,   n2,   n3);
}
}
}
发表于:2007-01-20 22:30:5016楼 得分:0
用全排列的算法,再对结果进行判断!
发表于:2007-01-20 23:03:0117楼 得分:3
全排列的数法参考
木野狐的学习记录http://www.cnblogs.com/rchen/archive/2005/06/21/178268.html

有//为本人所写,其他来自上述blog
using   system;
using   system.collections.generic;
using   system.text;

namespace   cons012001
{
        class   program
        {          
                static   void   main(string[]   args)
                {
                        char[]   s   =   "123456789 ".tochararray();
                        totalsort(s,   0);                      
                        console.readline();

                }
                static   int   resultcount   =   0;

                public   static   void   totalsort(char[]   list,   int   start)
                {
                    int   end   =   list.length   -   1;
     
                        if   (start   ==   end)
                        {
                                resultcount++;
                  string   ss   =new   string(list); //1
                  int   a   =   convert.toint16(ss.substring(0,   3));//2
                  int   b   =   convert.toint16(ss.substring(3,   3));//3
                  int   c   =   convert.toint16(ss.substring(6,   3));//4

                  if   (a   *   2   ==   b)     //5
{   if   (a   *   3   ==   c)                   console.writeline(a.tostring()+ "   "+b.tostring()     + "   "+c.tostring());   }       //6
                        }
                        else
                        {
                                for   (int   i   =   start;   i   <=   end;   i++)
                                {
                                        char[]   temp   =   new   char[list.length];
                                        list.copyto(temp,   0);
                                        char   tempc   =   temp[start];
                                        temp[start]   =   temp[i];
                                        temp[i]   =   tempc;
                  if   (convert.toint16(new   string(list).substring(0,   1))   <=3   )     //7(关键)
totalsort(temp,   start   +   1);
                                }
                        }
                }    

        }
}
发表于:2007-01-20 23:03:3718楼 得分:0
192     384     576
219     438     657
273     546     819
327     654     981
发表于:2007-01-21 16:27:0019楼 得分:0
sign
发表于:2007-01-21 20:40:2920楼 得分:0
..揭贴了额~
  不然发个效率很好的算法来~
发表于:2007-01-22 01:54:2821楼 得分:0
哎,还用想嘛!简单算法
一个等比数列,至于不要零就更简单了,无非是初值里不能含0含5
ok
111,222,333
112,224,336
发表于:2007-01-22 14:16:5122楼 得分:0
#include   <stdio.h>
#include   <conio.h>

main()
{
int   n1,n2,n3,n4,n5,n6,n7,n8,n9;
int   x=0,y=0,z=0;
for(n1=1;n1 <=9;n1++)
{
for(n2=1;n2 <=9;n2++)
{
if(n2==n1)
continue;
for(n3=1;n3 <=9;n3++)
{
if((n3==n1) ¦ ¦(n3==n2))
continue;
x=n1*100+n2*10+n3;
for(n4=1;n4 <=9;n4++)
{
if((n4==n1) ¦ ¦(n4==n2) ¦ ¦(n4==n3))
continue;
for(n5=1;n5 <=9;n5++)
{
if((n5==n1) ¦ ¦(n5==n2) ¦ ¦(n5==n3) ¦ ¦(n5==n4))
continue;
for(n6=1;n6 <=9;n6++)
{
if((n6==n1) ¦ ¦(n6==n2) ¦ ¦(n6==n3) ¦ ¦(n6==n4) ¦ ¦(n6==n5))
continue;
y=n4*100+n5*10+n6;
for(n7=1;n7 <=9;n7++)
{
if((n7==n1) ¦ ¦(n7==n2) ¦ ¦(n7==n3) ¦ ¦(n7==n4) ¦ ¦(n7==n5) ¦ ¦(n7==n6))
continue;
for(n8=1;n8 <=9;n8++)
{
if((n8==n1) ¦ ¦(n8==n2) ¦ ¦(n8==n3) ¦ ¦(n8==n4) ¦ ¦(n8==n5) ¦ ¦(n8==n6) ¦ ¦(n8==n7))
continue;
for(n9=1;n9 <=9;n9++)
{
if((n9==n1) ¦ ¦(n9==n2) ¦ ¦(n9==n3) ¦ ¦(n9==n4) ¦ ¦(n9==n5) ¦ ¦(n9==n6) ¦ ¦(n9==n7) ¦ ¦(n9==n8))
continue;
z=n7*100+n8*10+n9;
if((y==(2*x))&&(z==(3*x)))
{
printf( "%d:%d:%d\r\n ",x,y,z);
getch();
}
}
}
}
}
}
}
}
}
}
}
结果:
192     384     576
219     438     657
273     546     819
327     654     981
发表于:2007-01-22 14:18:4923楼 得分:0
#include   <stdio.h>
#include   <conio.h>

main()
{
  int   n1,n2,n3,n4,n5,n6,n7,n8,n9;
  int   x=0,y=0,z=0;
  for(n1=1;n1 <=9;n1++)
  {
    for(n2=1;n2 <=9;n2++)
    {
      if(n2==n1)
        continue;
      for(n3=1;n3 <=9;n3++)
      {
        if((n3==n1) ¦ ¦(n3==n2))
          continue;
        x=n1*100+n2*10+n3;
        for(n4=1;n4 <=9;n4++)
        {
          if((n4==n1) ¦ ¦(n4==n2) ¦ ¦(n4==n3))
            continue;
          for(n5=1;n5 <=9;n5++)
          {
            if((n5==n1) ¦ ¦(n5==n2) ¦ ¦(n5==n3) ¦ ¦(n5==n4))
              continue;
            for(n6=1;n6 <=9;n6++)
            {
              if((n6==n1) ¦ ¦(n6==n2) ¦ ¦(n6==n3) ¦ ¦(n6==n4) ¦ ¦(n6==n5))
                continue;
              y=n4*100+n5*10+n6;
              for(n7=1;n7 <=9;n7++)
              {
                if((n7==n1) ¦ ¦(n7==n2) ¦ ¦(n7==n3) ¦ ¦(n7==n4) ¦ ¦(n7==n5) ¦ ¦(n7==n6))
                  continue;
                for(n8=1;n8 <=9;n8++)
                {
                  if((n8==n1) ¦ ¦(n8==n2) ¦ ¦(n8==n3) ¦ ¦(n8==n4) ¦ ¦(n8==n5) ¦ ¦(n8==n6) ¦ ¦(n8==n7))
                    continue;
                  for(n9=1;n9 <=9;n9++)
                  {
                    if((n9==n1) ¦ ¦(n9==n2) ¦ ¦(n9==n3) ¦ ¦(n9==n4) ¦ ¦(n9==n5) ¦ ¦(n9==n6) ¦ ¦(n9==n7) ¦ ¦(n9==n8))
                      continue;
                      z=n7*100+n8*10+n9;
                    if((y==(2*x))&&(z==(3*x)))
                    {
                      printf( "%d:%d:%d\r\n ",x,y,z);
                      getch();
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
结果:
192     384     576
219     438     657
273     546     819
327     654     981
发表于:2007-01-22 14:55:4724楼 得分:0
穷举??????
发表于:2007-01-23 10:30:4225楼 得分:0
从他楼主的意思来说的话:
        第一个数是从123到329,这样一来的话我就把这206个数做个for循环,循环的目的就是把这206个对应的第二,第三个数组成一个9位的数(123246369),再进行一个if判断,把每一位的数相加看等于45不(1到9不重复的话相加就是45),这样就会得到21组每位相加为45的9位数。最后对这21组数进行一个if判断,把每一位的数相乘看等于362880不(1到9不重复的话相加就是362880),最后得到的就是楼上的结果。
        下面是我用vb写的一段代码,望大家指点指点:
      private   sub   form_load()
            dim   a,   b,   c,   d,   e,   f,   g   as   integer
      end   sub
      private   sub   form_paint()
          for   a   =   123   to   329
              g   =   0
              b   =   a   *   2
              c   =   a   *   3
              d   =   a   &   b   &   c
              for   e   =   1   to   9
                    g   =   g   +   mid(d,   e,   1)
              next   e
              if   g   =   45   then
                    if   mid(d,   1,   1)   *   mid(d,   2,   1)   *   mid(d,   3,   1)   *   mid(d,   4,   1)   *   mid(d,   5,   1)   *   mid(d,   6,   1)   *   mid(d,   7,   1)   *   mid(d,   8,   1)   *   mid(d,   9,   1)   <>   362880   then      
                    else
                          print   a;   "       "   &   b;   "       "   &   c
                    end   if
              else
              end   if
          next   a
      end   sub


快速检索

最新资讯
热门点击