P1005 continues (3n + 1) guess

  ┗ | `O '| ┛ wailing ~ ~, this damn guess again, difficult to upgrade, had not written as P100 recommendations 1 to take a look. Man of few words said, look at the title.

  Ah, This question is not so clear, uh, I can not say, actually quite clear. It is to let you figure out but did not appear in the Kharazi guess calculation process had occurred in the number of samples in the input. The idea is very clear, I do not think that, if we want to know which number does not appear, then you have to find the number of process Kharazi guess every calculus appears, and then recorded. This idea represents our Kharazi not reduce the number of calculations, can only be done from memory and comparing optimization, and if I have put every number there have been several are kept together, then we are not only a waste of space, but does not appear to find an array when the number is also very time consuming. In order to reduce the cost of time and space, we decided to adopt count sort of thinking to solve this problem.

  

Callatz void (int num) 
{ 
    while (it! = 1) 
    { 
        (whether% 2)? (num = (3 * num + 1) / 2): (whether he / = 2); 
        if (num <= 100) 
        { 
            FlagArray [whether] ++; // 
        } 
    } 
}

    This is the counting procedure, we open up an array length of more than 100, each step number calculation process will be occurring FlagArray [num] ++; recorded. When we were looking for just need to find out from the array to find FlagArray of 1 ~ n for the number to zero.

The idea is relatively simple to achieve a bit of trouble

Here is the code:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAX_SIZE 110
 4 int FlagArray[MAX_SIZE];
 5 void Callatz(int num);
 6 int cmp(const void *a, const void *b);
 7 
 8 int main()
 9 {
10     int n;
11     int NumsArray[MAX_SIZE], ans[MAX_SIZE];
12     scanf("%d", &n);
13 
14     for (int i = 0; i < n; i++)
15     {
16         scanf("%d", &NumsArray[i]);
17         Callatz(NumsArray[i]);
18     }
19     int j = 0;
20     for (int i = 0; i < n; i++)
21     {
22         if (!FlagArray[NumsArray[i]])
23         {
24             ans[j++] = NumsArray[i];
25             printf("%d ", ans[j-1]);
26         }
27     }
28     putchar('\n');
29     qsort(ans, j, sizeof(int), cmp);
30     for (int i = 0; i < j; i++)
31     {
32         printf("%s%d", i ? " " : "", ans[i]);
33     }
34     putchar(" \ N " );
35  
36      return  0 ;
37  }
 38  
39  void Callatz ( int num)
 40  {
 41      while (it! = 1 )
 42      {
 43          (Surely% 2 )? (num = ( 3 * num + 1 ) / 2 ): (whether he / = 2 );
44          if (num <= 100 )
 45          {
 46              FlagArray [whether] ++ ;
47         }
48     }
49 }
50 
51 int cmp(const void *a, const void *b)
52 {
53     return *(int *)b - *(int *)a;
54 }
View Code

  - algorithm is not easy, gentlemen encourage each other!

 

Guess you like

Origin www.cnblogs.com/daker-code/p/11619035.html