usaco1.34Prime Cryptarithm

之前做的一道题,忘记发了,纠结了好久,把题意想复杂了,就是直接枚举*号位置上的数。

View Code
 1 /*

 2  ID: your_id_here

 3  PROG: crypt1

 4  LANG: C++

 5  */

 6 #include <iostream>

 7 #include<cstdio>

 8 #include<string.h>

 9 using namespace std;

10 int count,f[100];

11 int judge(int s)

12 {

13     int i,flag = 1,m = s;

14     while(s)

15     {

16         i = s%10;

17         s = s/10;

18         if(!f[i])

19         {

20             flag = 0;

21             break;

22         }

23     }

24     if(flag)

25     return 1;

26     return 0;

27 }

28 int main()

29 {

30     freopen("crypt1.in","r",stdin);

31     freopen("crypt1.out","w",stdout);

32     int n,i,j,k,g,h,num[15];

33     cin>>n;

34     count = 0;

35     for(i = 1 ; i <= n ; i++)

36     {

37         cin>>num[i];

38         f[num[i]] = 1;

39     }

40     for(i = 1; i <= n ; i++)

41     for(j = 1;j <= n; j++)

42     for(k = 1; k <= n ; k++)

43     {

44         int x = num[i]+num[j]*10+num[k]*100;

45         for(g = 1 ; g <= n ; g++)

46         for(h = 1; h <= n ; h++)

47         {

48             int s1 = x*num[g];

49             int s2 = x*num[h];

50             int y = num[g]+num[h]*10;

51             int s = x*y;

52             if(judge(s1)&&judge(s2)&&judge(s))

53             {

54                 if(s<10000&&s>=1000&&s1>=100&&s1<1000&&s2>=100&&s2<1000)

55                     count++;

56             }

57         }

58     }

59     cout<<count<<endl;

60     fclose(stdin);

61     fclose(stdout);

62     return 0;

63 }

 

你可能感兴趣的:(USACO)