Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Node {
  6.     int index;
  7.     char item;
  8.     struct Node *next;
  9. } node;
  10.  
  11. void compare (node *head) {
  12.     int start, end, flag = 0, maior = 0, index_i, index_f;
  13.  
  14.     while (head != NULL) {
  15.         //printf ("head->item=%d flag=%d start=%d end=%d maior=%d index_i=%d index_f=%d\n", head->item, flag, start, end, maior, index_i, index_f);
  16.         if (head->item == 48) { //ZERO
  17.             flag++; //a flag é pra marcar o início da sequência de zero(s)
  18.  
  19.             if (flag == 1) start = head->index; //início da seq
  20.             end = head->index; //fim da seq, que vai rodar enquanto ler zero
  21.         }
  22.         if (head->item == 49 || head->next == NULL) { //UM OU O FIM
  23.             int dif = end - start; //printf("dif=%d\n", dif);
  24.  
  25.             if (dif > maior) {
  26.                 maior = dif;
  27.                 index_i = start;
  28.                 index_f = end;
  29.             }
  30.             flag = 0;
  31.         }
  32.         head = head->next;
  33.     }
  34.     printf ("%d %d\n", index_i, index_f);
  35. }
  36.  
  37. void print_list (node *head) {
  38.     while (head != NULL) {
  39.         printf ("%d\n", head->item); //printf ("%d\n", head->index);
  40.         head = head->next;
  41.     }
  42. }
  43.  
  44. node* add_end (node *head, char item, int i) {
  45.     node *new_node = (node*) malloc (sizeof(node));
  46.     new_node->item = item;
  47.     new_node->next = NULL;
  48.     new_node->index = i;
  49.  
  50.     if (head == NULL) return new_node;
  51.     node *current = head;
  52.  
  53.     while (current->next != NULL) current = current->next;
  54.  
  55.     current->next = new_node;
  56.     return head;
  57. }
  58.  
  59. int main() {
  60.     node *head = NULL;
  61.     char str [10000];
  62.     int i;
  63.  
  64.     while (1) {
  65.         scanf (" %[^\n]s", str);
  66.  
  67.         if (strcmp(str, "0") == 0) break;
  68.  
  69.         for (i = 0; i < strlen(str); i++) {
  70.             head = add_end (head, str[i], i); //printf ("%d\n", head);
  71.         }
  72.         //print_list (head);
  73.         compare (head);
  74.         head = NULL;
  75.     }
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement