Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Node
  6. {
  7.     int data;
  8.     struct Node* next;
  9. } Node;
  10.  
  11. Node* head;
  12.  
  13. void compare(int biggest, int ini, int end, int aux, int posiAt, int i)
  14. {
  15.     int r1, r2;
  16.  
  17.     Node* temp = head;
  18.     while(temp != NULL)
  19.     {
  20.         if(temp->data == '0') // o valor atual é 0
  21.         {
  22.             if(i = 0) // estou começando o streak de 0's
  23.             {
  24.                 ini = posiAt; // posição inicial da sequência recebe o valor da posição atual da lista
  25.             }
  26.            
  27.             i++; // achei um 0, i++
  28.             aux = i; // aux recebe a quantidade atual de 0's
  29.             posiAt++; // avanço na posição
  30.         }
  31.         else // o valor atual é 1
  32.         {
  33.             if(i > 0) // se i for maior que 0, significa que eu to em um streak de 0's
  34.             {
  35.                 end = posiAt; // posição final da sequencia recebe o valor da posição atual da lista
  36.  
  37.                 if(aux > biggest)
  38.                 {
  39.                     biggest = aux;
  40.                     r1 = ini;
  41.                     r2 = end;
  42.                 }
  43.  
  44.                 i = 0;  // reseto a quantiade de i's achadas
  45.             }
  46.  
  47.             posiAt++;
  48.         }
  49.  
  50.         temp = temp->next;
  51.     }
  52.  
  53.     printf("%d %d\n", r1, r2);
  54. }
  55.  
  56. void reverse()
  57. {
  58.     Node* current = head;
  59.     Node *prev = NULL, *next = NULL;
  60.  
  61.     while (current != NULL)
  62.     {
  63.  
  64.         next = current->next;
  65.  
  66.         current->next = prev;
  67.  
  68.         prev = current;
  69.         current = next;
  70.     }
  71.  
  72.     head = prev;
  73. }
  74.  
  75. void print()                                                    //Função que printa os dados
  76. {
  77.     Node* temp = head;
  78.  
  79.     printf("A Lista e: ");
  80.  
  81.     while(temp != NULL)                                         //Enquanto a lista não atingir o fim, continue printando.
  82.     {
  83.         printf(" %d", temp->data);
  84.         temp = temp->next;
  85.     }
  86.  
  87.     printf("\n");
  88. }
  89.  
  90.  
  91. void insert(char x)
  92. {
  93.     Node* temp = (Node*)malloc(sizeof(Node));
  94.  
  95.     temp->data = x;
  96.     temp->next = head;
  97.     head = temp;
  98. }
  99.  
  100. int main()
  101. {
  102.     head = NULL;
  103.  
  104.     int i, size;
  105.     char seq[9999];
  106.  
  107.     while(1) // enquanto for verdadeiro
  108.     {
  109.         scanf(" %[^\n]s", seq);
  110.  
  111.         size = strlen(seq);
  112.  
  113.         if(size == 1 && seq[0] == '0') // a entrada foi '0'?
  114.         {  
  115.             return 0; // se sim, pare de ler
  116.         }
  117.         else // caso contrário, pegue a entrada e bote na lista
  118.         {
  119.             for(i = 0; i < size; i++)
  120.             {
  121.                 if(seq[i] == 49)
  122.                 {
  123.                     insert(1);
  124.                 }
  125.                 else
  126.                 {
  127.                     insert(0);
  128.                 }
  129.             }
  130.  
  131.             reverse();
  132.            
  133.             compare(-1, 0, 0, 0, 0, 0);
  134.         }  
  135.     }
  136. }
  137.  
  138.  
  139. // bug atual: bug na contagem de inical e final.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement