Algorithm to Improve Lottery

Resource constraints
Time limit: 1.0s Memory Limit: 256.0MB
Problem Description
  To enrich male Festival, expensive based lottery female part is provided, rules are as follows:
  1, each lottery ticket printed on seven different numbers, and these numbers are in the range [1, 33];
  2, each times before awarding the winning numbers will be announced by the seven different from each other a number consisting of;
  3, were set up seven prizes, grand prize and first prize to Liu Dengjiang. Duijiang rules are as follows:
  Grand Prize: Request seven numbers appear in the lottery winning numbers in;
  first prize: Requires six numbers in the lottery winning numbers appear in;
  second prize: five numbers appear on the requirements of the lottery in the winning numbers;
  ......
  Liu Dengjiang: numbers in claim 1 appears in the winning lottery numbers;
  Note: the order does not appear in number, for example, if the winning number is 2331114191718, the lottery 128 9,231,167 due to which there are two numbers (23 and 1) appear in the winning numbers in the lottery so the fifth prize.
  It is known that a number of winning numbers and lottery numbers to buy Li Hua, please write a program to determine his lottery jackpot.
Input Format
  The first line of a positive integer n, the number of lottery tickets, a second line of seven integer representing the winning numbers, integers n for each row line 7 following, n lottery.
Output Format
  7 space separated numbers, the first number indicates the number of sheets of winning the grand prize, the first two numbers indicate the number of sheets of winning the first prize, the first three numbers indicate the number of winning second prize in the 7th ...... winning numbers indicate the number of sheets Liu Dengjiang.
Sample input
3
1 2 3 4 5 6 7
11 12 13 14 15 16 17
12 13 14 15 16 17 18
8 7 10 9 31 30 29
Sample Output
0 0 0 0 0 0 1
Scale data and conventions
  30% of the data of n-<= 100;
  70% of the data n-<= 1000;
  100% data n <= 100000.

  ***** Note: the array is defined as a global variable, you can allocate more memory. *****
A thought: to open up an array of violent iteration
 1 #include<stdio.h>
 2 
 3 int lotteryNum[100000][7] = {0};
 4 
 5 int main()
 6 {
 7     int n;
 8     int prizeNumer[7] = {0};
 9     int prizeYorN[7] = {0};
10     int i,j,k;
11 
12     scanf("%d", &n);
13     for (i = 0; i < 7; i++)
14     {
15         scanf("%d", &prizeNumer[i]);
16     }
17     for (i = 0; i < n; i++)
18     {
19         for (j = 0; j < 7; j++)
20         {
21             scanf("%d", &lotteryNum[i][j]);
22         }
23     }
24     
25     for (i = 0; I <n-; I ++)   // a cycle has several lottery 
26 is      {
 27          int ANS = 0 ;
 28          for (J = 0 ; J < . 7 ; J ++)   // one by one than 
29          {
 30              for (K = 0 ; K < . 7 ; K ++)   // for each lottery ticket traverse 
31 is              {
 32                  IF (prizeNumer [J] == lotteryNum [I] [K])
 33 is                  {
 34 is                      ANS ++ ;
 35                  }
 36              }
37          }
 38          switch (years)
 39          {
 40          case  7 : prizeYorN [ 0 ] + = 1 ;
41              break ;
42          box  6 : prizeYorN [ 1 ] + = 1 ;
43              break ;
44          box  5 : prizeYorN [ 2 ] + = 1 ;
45              break ;
46          box  4 : prizeYorN [ 3 ] + = 1 ;
47              break ;
48         case 3: prizeYorN[4] += 1;
49             break;
50         case 2: prizeYorN[5] += 1;
51             break;
52         case 1: prizeYorN[6] += 1;
53         }
54     }
55 
56     for (i = 0; i < 7; i++)
57     {
58         printf("%d ", prizeYorN[i]);
59     }
60     return 0;
61 }

Thinking two: no need to open up large arrays, open up a whole array of f is initialized to 1, and then only once traversed the lottery, direct look-up table, if  f [i] == 1 , the  SUM ++ , and then use the p [sum] counts, finally reverse output p [sum].

Run time complexity two ideas are O (N), but significantly less memory usage second idea.

 

 1 #include<stdio.h>
 2 
 3 int f[33], p[10], n, m;
 4 int main()
 5 {
 6     scanf("%d",&n);
 7     for (int i = 0; i < 7; i++)
 8     {
 9         scanf("%d", &m);
10         f[m] = 1;
11     }
12     for (int i = 1; i <= n; i++)
13     {
14         int sum = 0;//中奖号码数量
15         for (int j = 1; j <= 7; j++)
16         {
17             scanf("%d", &m);
18             if (f[m] == 1)
19                 sum++;
20         }
21         p[sum]++;
22     }
23     for (int i = 7; i >= 1; i--)
24         printf("%d", p[i]);
25     return 0;
26 }

 

 

 

Guess you like

Origin www.cnblogs.com/ZhengLijie/p/12652004.html