USACO-Chapter1-Section 1.3-Prime Cryptarithm (crypt1)

【题目描述】

      下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。

        * * *
    x     * *
   ----------
        * * *
      * * *
   ----------
      * * * *

数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。

注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.

写一个程序找出所有的牛式。

【输入格式】

      Line 1:数字的个数n。 Line 2:N个用空格分开的数字(每个数字都属于{1,2,3,4,5,6,7,8,9})。

【输出格式】

      共一行,一个数字。表示牛式的总数。

【输入样例】

5
2 3 4 6 8

【输出样例】

1
【样例分析】
        2 2 2
    x     2 2
   ----------
        4 4 4
      4 4 4
   ----------
      4 8 8 4

注意:结果只能为4位

【思路】

      枚举,枚举每一位,先判断是否结果合法,在判断脱式中的每项是否合法。

【代码】

  • Executing...
  • Test 1: TEST OK [0.000 secs, 276 KB]
  • Test 2: TEST OK [0.000 secs, 276 KB]
  • Test 3: TEST OK [0.000 secs, 276 KB]
  • Test 4: TEST OK [0.000 secs, 276 KB]
  • Test 5: TEST OK [0.000 secs, 276 KB]
  • Test 6: TEST OK [0.000 secs, 276 KB]
  • Test 7: TEST OK [0.000 secs, 276 KB]
  • All tests OK.
{
 ID  : c_CaM.19
 LANG: PASCAL
 TASK: crypt1
}
Program CaM(input,output);
Var
  a:array[0..10] of byte;
  flag:set of byte;
  i,j,k,l,p,n,t,ss,s,ans,tt:longint;

Procedure innt;
Begin
  assign(input,'crypt1.in'); reset(input);
  assign(output,'crypt1.out'); rewrite(output);
End;

Procedure outt;
Begin
  close(input);
  close(output);
End;

Begin
  innt;

  readln(n);
  for i:=1 to n do
  Begin
    read(a[i]);
    flag:=flag+[a[i]];
  End;

  for i:=1 to n do
    for j:=1 to n do
      for k:=1 to n do
        for l:=1 to n do
          for p:=1 to n do
          Begin
            s:=a[i]*100+a[j]*10+a[k]; ss:=a[l]*10+a[p];
            t:=s*ss;
            if (t>1000)and(t<10000) then
              if (t div 1000) in flag then
                if ((t div 100) mod 10) in flag then
                  if ((t div 10)mod 10) in flag then
                    if (t mod 10) in flag then
                    Begin
                      t:=s*a[p]; tt:=s*a[l];
                      if (t mod 10) in flag then
                        if ((t div 10)mod 10) in flag then
                          if (t div 100) in flag then
                            if tt mod 10 in flag then
                              if ((tt div 10)mod 10) in flag then
                                if (tt div 100) in flag then
                                  inc(ans);
                    End;
          End;
  writeln(ans);

  outt;
End.
posted @ 2012-02-21 23:58  你滴韩王  阅读(311)  评论(0)    收藏  举报