root/misc/sequenza.c

Revision 966, 1.3 kB (checked in by alpt, 2 years ago)

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* AlpT
2  * Thu Jul 14 01:12:10 CEST 2005
3  * Mazara del Vallo
4  *
5  * sequenza.c:
6  * Given a list of numbers separated by a '\n' in stdin, it finds (if any) the
7  * cyclic sequences in the list and it prints them:
8  * 54 6 9 2 1 9 2 1 54 6 9 2 1 9 2 1 54 6 9 2 1 9 2 1 54 6 9 2 1 9 2 1
9  * In the above case the cyclic sequence is:
10  * 54 6 9 2 1 9 2 1
11  *
12  * In the worst case the O-big notation of the algorithm is:
13  * O(n + n/2 + n/3 + n/... + n/n), where n is the number of numbers in the
14  * list.
15  */
16
17 #include <stdio.h>
18
19 int main()
20 {
21         int n,i, alloced=500, e, o, x;
22         int *seq;
23         char s[19];
24
25         n=0;
26         seq=(int *)malloc(alloced*sizeof(int));
27         while(fgets(s, sizeof(s), stdin)) {
28                 if(s[0] == '\n')
29                         continue;
30                
31                 seq[n]=atoi(s);
32 //              printf("seq[%d]: %d\n", n, seq[n]);
33                 n++;
34                 if(n == alloced) {
35                         alloced*=2;
36                         seq=(int *)realloc(seq, alloced*sizeof(int));
37                 }
38         }
39
40         if(!n)
41                 goto finish;
42
43         /*
44          * find sequence
45          */
46
47 //      printf("n: %d\n",n);
48         for(i=1; i <= n/2; i++) {
49 //              printf("i: %d\n",i);
50                 o=0;   
51                 for(e=1; e*i <= n-i; e++) {
52 //                      printf("e: %d\n",e);
53                         for(x=0; x<i; x++) {
54 //                              printf("x: %d. a:%d, b:%d\n", x,seq[x],seq[x+e*i]);
55                                 if(seq[x] != seq[x+e*i])
56                                         break;
57                         }
58                         if(x < i) {
59                                 o=1;
60                                 break;
61                         }
62                 }
63
64                 if(!o) {
65                         for(o=0; o < i; o++)
66                                 printf("%d ", seq[o]);
67                         printf("\n");
68                 }
69         }
70
71 finish:
72         free(seq);
73 }
Note: See TracBrowser for help on using the browser.