题目连接:
http://ace.delos.com/usacoprob2?a=xWwmrH0ntr1&S=crypt1
The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.
* * * x * * ------- * * * <-- partial product 1 * * * <-- partial product 2 ------- * * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.
Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.
Write a program that will find all solutions to the cryptarithm above for any subset of digits from the set {1,2,3,4,5,6,7,8,9}.
PROGRAM NAME: crypt1
INPUT FORMAT
Line 1: | N, the number of digits that will be used |
Line 2: | N space separated digits with which to solve the cryptarithm |
SAMPLE INPUT (file crypt1.in)
5 2 3 4 6 8
OUTPUT FORMAT
A single line with the total number of unique solutions. Here is the single solution for the sample input:
2 2 2 x 2 2 ------ 4 4 4 4 4 4 --------- 4 8 8 4
SAMPLE OUTPUT (file crypt1.out)
1
题目翻译:
描述
下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。
* * * x * * ---------- * * * * * * ---------- * * * *
数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。
注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.
写一个程序找出所有的牛式。
[编辑] 格式
PROGRAM NAME: crypt1
INPUT FORMAT:
(file crypt1.in)
Line 1:数字的个数n。 Line 2:N个用空格分开的数字(每个数字都属于{1,2,3,4,5,6,7,8,9})。
OUTPUT FORMAT:
(file crypt1.out)
共一行,一个数字。表示牛式的总数。
[编辑] SAMPLE INPUT
5 2 3 4 6 8
[编辑] SAMPLE OUTPUT
1
[编辑] 样例分析
2 2 2 x 2 2 ---------- 4 4 4 4 4 4 ---------- 4 8 8 4
/*
ID:smilecl1
LANG:C
TASK:crypt1
*/
/*
TIME:2012/11/12
EMAIL: SmileCloud201@gmail.com
NAME: LiYun
*/
/*
* 思路: 用5层for循环来完成,如果条件不满足,立刻进入下一次循环,缩短执行时间。
*
*/
#include <stdio.h>
#define max_number 10
int is_set( int m, int *p, int n)
{
int i =0;
for ( i = 0; i < n; ++i )
{
if ( m == p[i] ) return 1;
}
return 0;
}
int main()
{
FILE *fin;
FILE *fout;
int set[max_number] = {0};
int number;
int i1, i2, i3, i4, i5;
int i = 0;
int j = 0;
int a[3];
int b[2];
int c[3];
int d[3];
int e[4];
int flag = 0;
int number1;
int result1;
int result2;
int result;
int count = 0;
fin = fopen("crypt1.in", "r");
fout = fopen("crypt1.out", "w");
fscanf(fin, "%d", &number);
for ( i = 0; i < number; ++i)
{
fscanf( fin, "%d", &set[i]);
}
for ( i1 = 0; i1 < number; ++i1)
{
a[0] = set[i1];
for ( i2 = 0; i2 < number; ++i2)
{
a[1] = set[i2];
for ( i3 = 0; i3 < number; ++i3)
{
a[2] = set[i3];
for ( i4 = 0; i4 < number; ++i4)
{
b[0] = set[i4];
for ( i5 = 0; i5 < number; ++i5)
{
b[1] = set[i5];
number1 = a[0] + a[1] * 10 + a[2] * 100;
result1 = number1 * b[0];
result2 = number1 * b[1];
result = result1 + result2 * 10;
flag = 1;
if ( result1 / 1000 || result2 / 1000 || result / 10000 )
{
flag = 0;
continue;
}
for ( j = 0; j < 3; ++j )
{
c[j] = result1 % 10;
d[j] = result2 % 10;
if ( !is_set( c[j], set, number ) || !is_set( d[j], set, number ) )
{
flag = 0;
break;
}
result1 /= 10;
result2 /= 10;
}
/* 上面的break只能结束一层for循环,实际上只要flag=0发生过一次,就已经不满足条件了,
就没有必要进行后面的判断,提前结束程序。 */
if ( 0 == flag) continue;
for ( j = 0; j < 4; ++j )
{
e[j] = result % 10;
if ( !is_set( e[j], set, number ) )
{
flag = 0;
break;
}
result /= 10;
}
if ( flag ) count++;
}
}
}
}
}
fprintf( fout, "%d\n", count);
return 0;
}