您的位置:首页 > 其它

【模拟】洛谷 P1211 [USACO1.3]牛式 Prime Cryptarithm

2017-05-08 11:14 567 查看

题目描述

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



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

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

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

输入输出格式

输入格式:

Line 1:数字的个数n。

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

输出格式:

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

输入输出样例

输入样例#1:

5

2 3 4 6 8

输出样例#1:

1

说明

题目翻译来自NOCOW。

USACO Training Section 1.3

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool flag[10];
int sum;
void read(int &x)
{
x=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
}
int main()
{
int n;
read(n);
for(int i=1;i<=n;++i)
{
int x;
read(x);
flag[x]=true;
}
for(register int i=1;i<=9;++i)
{
if(flag[i])
{
for(register int j=1;j<=9;++j)
{
if(flag[j])
{
for(register int k=1;k<=9;k++)
{
if(flag[k])
{
for(register int m=1;m<=9;m++)
{
if(flag[m])
{
for(register int n=1;n<=9;n++)
{
if(flag
)
{
bool p=true;
int s1=i*100+j*10+k,s2=m*10+n;
int temp=s1*m;
while(temp>0)
{
if(temp>999)
{
p=false;
break;
}
int x=temp%10;
if(!flag[x])
{
p=false;
break;
}
temp/=10;
}
if(!p)continue;
temp=s1*n;
while(temp>0)
{
if(temp>999)
{
p=false;
break;
}
int x=temp%10;
if(!flag[x])
{
p=false;
break;
}
temp/=10;
}
if(!p)continue;
temp=s1*s2;
while(temp>0)
{
if(temp>9999)
{
p=false;
break;
}
int x=temp%10;
if(!flag[x])
{
p=false;
break;
}
temp/=10;
}
if(!p)continue;
sum++;
}
}
}
}
}
}
}
}
}
}
printf("%d",sum);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  洛谷 模拟