The number of cattle type

 

The following is a vertical multiplication, if we instead use the given numbers that n *, the equation can be established, we call this formula cow style.

      * * *
   x    * *
    -------
      * * *
    * * *
    -------
    * * * *

* Digital can only replace, of course, the first can not be 0. Write a program to find all type of cattle.

Entry

The first line, the number of digits n.
Second line: N numbers separated by spaces (each number ∈ {1,2,3,4,5,6,7,8,9}).

Export

  Total row, a number. It represents the total number of cattle style. The following is a sample of the type of cattle.

      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

Ideas:

1, enumeration. One-dimensional array to store three-digit, two-dimensional array of storage-digit

2, verification violence.

Problems encountered:

1, the answer is wrong. Since the three-digit number without thoughtful, ABB and AAB situation is taken into account. Finally, simply triplet (0-n) cycles

2, a runtime error. One-dimensional array of small space opened up, leading to cross-border stored and accessed.

 

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 int n_nums = 0;
 7 int n_up = 0, n_down = 0;
 8 
 9 void Init(int nums[],int nUp[],int nDown[][2]);
10 bool InNums(int tp,int nums[]);
11 bool IsOk(int tp,int nums[]);
12 void Solve();
13 
14 int main()
15 {
16     Solve();
17     return 0;
18 }
19 void Solve()
20 {
21     //9225
22     int nUp[9226],nums[10];
23     int nDown[82][2];
24     Init(nums,nUp,nDown);
25     int ans = 0;
26     int i, j;
27     int tp1, tp2, tp3;
28     bool b1, b2;
29     for (i = 0; i < n_up; i++)
30     {
31         for (j = 0; j < n_down; j++)
32         {
33             tp1 = nDown[j][0] * nUp[i];
34             tp2 = nDown[j][1] * nUp[i];
35             tp3 = nUp[i] * (nDown[j][0] * 10 + nDown[j][1]);
36             b1 = (tp1 <= 999 && tp1 >= 100 && tp2 <= 999 && tp2 >= 100) ? 1 : 0;
37             b2 = (tp3 <= 9999 && tp3 >= 1000) ? 1 : 0;
38             if (b1&&b2)
39             {
40                 if (IsOk(tp1,nums) && IsOk(tp2,nums) && IsOk(tp3,nums)) {
41                     ans++;
42                 }
43             }
44         }
45     }
46     cout << ans << endl;
47 }
48 void Init(int nums[],int nUp[],int nDown[][2])
49 {
50     int n, i, j, k, get;
51     cin >> n;
52     for (i = 0; i < n; i++)
53     {
54         cin >> get;
55         if (!InNums(get,nums))
56         {
57             nums[n_nums++] = get;
58         }
59     }
60     sort(nums, nums + n_nums);
61     for(i=0; i<n_nums;i++)
62         for(j=0; j<n_nums; j++)
63             for(k=0; k<n_nums; k++)
64                 nUp[n_up++]=nums[i]*100+nums[j]*10+nums[k];
65     for(i=0; i<n_nums; i++)
66         for(j=0; j<n_nums; j++)
67         nDown[n_down][0]=nums[i],nDown[n_down++][1]=nums[j];
68 }
69 bool InNums(int tp,int nums[])
70 {
71     for (int i = 0; i < n_nums; i++)
72         if (tp == nums[i])
73             return true;
74     return false;
75 }
76 bool IsOk(int tp,int nums[])
77 {
78     int b = tp % 10;
79     while (tp)
80     {
81         if (!InNums(b,nums)) {
82             return false;
83         }
84         tp /= 10;
85         b = tp % 10;
86     }
87     return true;
88 }
View Code

 

Guess you like

Origin www.cnblogs.com/guoyujiang/p/11831755.html