欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~
Description
下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。
* * * x * * ------- * * * * * * ------- * * * *
数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。
注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.
写一个程序找出所有的牛式。
Input
Line 1:数字的个数n。 Line 2:N个用空格分开的数字(每个数字都∈ {1,2,3,4,5,6,7,8,9})。
Output
共一行,一个数字。表示牛式的总数。
下面是样例的那个牛式。
2 2 2 x 2 2 --------- 4 4 4 4 4 4 --------- 4 8 8 4
Sample Input
5 2 3 4 6 8
Sample Output
1
注意:
最需要注意的就是,要严格按照所给的内容和格式。
假设,三位数 x 的百、十、个位分别为 i 、j 、k,则 x = i*100+j*10+k,
同理,两位数 y 的十、个位分别为 p、q,则 y = p*10+q 。
1)必须是3位数*2位数(即100<= x <=999,10<= y <=99);
2) x 乘上 y 的个位(即x*q)必须为3位数;
3) x 乘上 y 的十位(即x*p)必须是4位数(最后一位的0省略了);
4)最后加和的结果必须是4位数。
5)式子中的所有数必须全部来自所给数字。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int n,a[10000000];
bool f(int x)
{
while(x)
{
bool flag=0;
int z=x%10;
for(int i=0;i<=n-1;i++)
if(z==a[i])
flag=1;
if(!flag)
return 0;
x/=10;
}
return 1;
}
int main()
{
int ans=0;
scanf("%d",&n);
for(int i=0; i<=n-1; i++)
scanf("%d",&a[i]);
for(int i=0; i<=n-1; i++)
for(int j=0; j<=n-1; j++)
for(int k=0; k<=n-1; k++)
for(int p=0; p<=n-1; p++)
for(int q=0; q<=n-1; q++)
{
int x=(a[i]*100+a[j]*10+a[k])*a[q];
if(x>999)
continue;
int y=(a[i]*100+a[j]*10+a[k])*a[p];
if(y>999)
continue;
int z=y*10+x;
if(z>9999)
continue;
if(f(x)&&f(y)&&f(z))
ans++;
}
printf("%d\n",ans);
return 0;
}