Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct node Node;
  6.  
  7. struct node {
  8.     char item;
  9.     Node *next;
  10. };
  11.  
  12. Node* insert_begin(Node *aux, char num) {
  13.     Node *new_node = (Node*) malloc(sizeof(Node));
  14.  
  15.     new_node->item =  num;
  16.     new_node->next = NULL;
  17.  
  18.     if (aux != NULL) aux->next = new_node;
  19.     return new_node;
  20. }
  21.  
  22. void highest_sequence(Node *head) {
  23.     int freq = 0, max = 0;
  24.     int aux_initial, aux_final, start, end;
  25.     int i = 0;
  26.  
  27.     while (head != NULL) {
  28.         if (head->item == '0') {
  29.             if (freq == 0) aux_initial = i;
  30.             aux_final = i;
  31.             freq++;
  32.         }
  33.         //printf(" frequen %d\n", freq);
  34.         if (head->item == '1' || head->next == NULL) {
  35.             if (freq > max) {
  36.                 max = freq;
  37.                 start = aux_initial;
  38.                 end = aux_final;
  39.             }
  40.             freq = 0;
  41.         }
  42.         i++;
  43.         head = head->next;
  44.     }
  45.     printf("%d %d\n", start, end);
  46. }
  47.  
  48. void print_list(Node *head) {
  49.  
  50.     while (head != NULL) {
  51.         printf("%c - > ", head->item);
  52.         head = head->next;
  53.     }
  54.     printf("\n");
  55. }
  56.  
  57. int main() {
  58.     Node *head = NULL;
  59.     Node *aux = NULL;
  60.     char string[1000];
  61.     int i;
  62.  
  63.     while (1) {
  64.         scanf("%s", string);
  65.         getchar(); // pega o \n.
  66.  
  67.         if (strcmp(string, "0") == 0) break;
  68.  
  69.         for (i = 0; i < strlen(string); i++) {
  70.             aux = insert_begin(aux, string[i]);
  71.             if (head == NULL) head = aux;
  72.         }
  73.         highest_sequence(head);
  74.         head = NULL;
  75.         //print_list(head);
  76.     }
  77.  
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement