【模拟】洛谷 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; }
相关文章推荐
- P1211 [USACO1.3]牛式 Prime Cryptarithm
- 【USACO-Chapter1-1.3】【模拟】Prime Cryptarithm
- 这压缩,太强了![USACO1.3]牛式 Prime Cryptarithm
- 洛谷P1211 [USACO1.3]牛式 Prime Cryptarithm
- [USACO1.3]牛式 Prime Cryptarithm
- USACO1.3牛式 Prime Cryptarithm
- 【USACO1.3】牛式 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- USACO - Chapter1 Section 1.3 - Prime Cryptarithm
- 【洛谷1211 [USACO1.3]牛式 Prime Cryptarithm】【枚举】
- USACO-Section1.3 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm (crypt1)
- USACO 1.3 Prime Cryptarithm
- USACO-Section 1.3 Prime Cryptarithm(枚举)
- USACO Section 1.3 : Prime Cryptarithm
- USACO Section 1.3 Prime Cryptarithm
- USACO-Section 1.3 Prime Cryptarithm[...]
- [USACO]Section 1.3 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm