Chinaunix首页 | 论坛 | 博客
  • 博客访问: 472278
  • 博文数量: 117
  • 博客积分: 3195
  • 博客等级: 中校
  • 技术积分: 1156
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 01:44
文章分类

全部博文(117)

文章存档

2012年(5)

2011年(5)

2010年(46)

2009年(61)

我的朋友

分类: C/C++

2010-04-28 03:16:28

将0,1,2,3,4,5,6,7,8,9共10个数字填入下面的乘法公式中, 代替*号, 每个数字只能用2次,总共20个*刚刚好。  

      *** (被乘数)
      *** (乘数)
  ------
      ***
    ***
  ***
--------
  ***** (积)


#include <stdio.h>
#include <string.h>
#include <time.h>

int main()
{
    int a, b, c, d, ok;
    int i, j, k, out=1;
    int cnt[10];
    
    clock_t s, t;
    s = clock();

    for(i=100; i<=299 && out; i++)
    {
        for(j=112; j<=998 && out; j++)
        {
            if(j%100/10 == 0 || j % 10 == 0 || i % 10 == 0)
                 continue;
            memset(cnt, 0, sizeof(cnt));
            cnt[i/100]++;
            cnt[i%100/10]++;
            cnt[i%10]++;

            cnt[j/100]++;
            cnt[j%100/10]++;
            cnt[j%10]++;

            a = j % 10 * i;
            if(a > 999)
                continue;
            cnt[a/100]++;
            cnt[a%100/10]++;
            cnt[a%10]++;

            b = j % 100 / 10 * i;
            if(b > 999)
                continue;
            cnt[b/100]++;
            cnt[b%100/10]++;
            cnt[b%10]++;

            c = j / 100 * i;
            if(c > 999)
                continue;
            cnt[c/100]++;
            cnt[c%100/10]++;
            cnt[c%10]++;

            d = a + b * 10 + c * 100;
            if(d < 100000)
            {
                cnt[d/10000]++;
                cnt[d%10000/1000]++;
                cnt[d%1000/100]++;
                cnt[d%100/10]++;
                cnt[d%10]++;

                ok = 1;
                for(k=0; k<=9; k++)
                {
                    if(cnt[k] > 2)
                    {
                        ok = 0;
                        break;
                    }
                }
             if(ok == 1)
                {
                    printf("%d %d\n", i, j);
                    out = 0;
                }
            }
        }
    }
    t = clock();
    printf("time: %dMS\n", t-s);
    return 0;
}


阅读(558) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chhaya2010-04-30 15:27:05

此题该如何秒杀呢??