[SRC_C++]abcde-fghij=20085

 玩一个黑客游戏时写的..要求计算出a,b,c,d,e,f,g,h,i,j的所有解.且a,b,c,d,e,f,g,h,i,j不可以重复.

算法一:

#include <iostream.h>
#include <conio.h>
#include <time.h>

void main()
{
  int a[10],b = 0;
  unsigned long Sum,LoopCount;
  clock_t start,finish;
 
  start = clock(); 
  cout<<"a,b,c,d,e,f,g,h,i,j,LoopCount"<<"/n";
 
  for (int i = 10000;i < 79914;i++)
  {
 b = 0;
 LoopCount += 1;
 
 Sum = i + 20085;
 a[0] = i / 10000;
 a[1] = (i / 1000) % 10;
 a[2] = (i / 100) % 10;
 a[3] = (i / 10) % 10;
 a[4] = i % 10;
 a[5] = Sum / 10000;
 a[6] = (Sum / 1000) % 10;
 a[7] = (Sum / 100) % 10;
 a[8] = (Sum / 10) % 10;
 a[9] = Sum % 10;

 for (int x = 0;x < 9;x++)

   for (int y = x + 1;y < 10;y++)
    if (a[x] == a[y])
  {
    x = 9;
    b = 1;
    break;
  }
 if (!b)
 {
   for (int x1 = 0;x1 < 10;x1++)
     cout<<a[x1]<<",";
   cout<<LoopCount<<"/n";
 }
  }
  finish = clock();
  cout<<"RunTime:"<<(finish - start)<<"ms,  LoopCount:"<<LoopCount<<"/nPress ENTER to continue../n";
  getch();
}

算法二:

#include <iostream.h>
#include <conio.h>
#include <time.h>


void main()
{
 int a,b,c,d,e,f,g,h,i,j;
 unsigned long LoopCount = 0;
 clock_t start,finish;
 
 start = clock();
 cout<<"a,b,c,d,e,f,g,h,i,j,LoopCount/n";
 for (a = 0;a <= 9;a++)    //a

  for (b = 0;b <= 9;b++)    //b
   if (b != a)

    for (c = 0;c <= 9;c++)      //c
     if (c != b && c != a)

      for (d = 0;d <= 9;d++)  //d
       if (d  != c && d != b && d != a)

        for (e = 0;e <= 9;e++) //e
         if (e != d && e != c && e != b && e != a)


          for (f = 0;f <= 9;f++) //f
           if (f != e && f != d && f != c && f != b && f != a)

            for (g = 0;g <= 9;g++) //g
             if (g != f && g != e && g != d && g != c && g != b && g != a)

              for (h = 0;h <= 9;h++) //h
               if (h != g && h != f && h != e && h != d && h != c && h != b && h != a)

                 for (i = 0;i <= 9;i++) //i
                  if (i != h && i != g && i != f && i != e && i != d && i != c && i != b && i != a)

                   for (j = 0;j <= 9;j++) //j
                    if (j != i && j != h && j != g && j != f && j != e && j != d && j != c && j != b && j != a)
                    {
                      LoopCount+=1;
                      if ((a * 10000 + b * 1000 + c * 100 + d * 10 + e + 20085) == (f * 10000 + g * 1000 + h * 100 + i * 10 + j))
                       cout<<a<<","<<b<<","<<c<<","<<d<<","<<e<<","<<f<<","<<g<<","<<h<<","<<i<<","<<j<<","<<LoopCount<<"/n";
                    }


  finish = clock();
  cout<<"time:"<<(finish - start)<<"ms,  LoopCount:"<<LoopCount<<"/nPress ENTER to continue../n";
  getch();
}
是个很笨重的算法..有没有大虾帮忙改进下..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值