Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Node {
- int index;
- char item;
- struct Node *next;
- } node;
- void compare (node *head) {
- int start, end, flag = 0, maior = 0, index_i, index_f;
- while (head != NULL) {
- //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);
- if (head->item == 48) { //ZERO
- flag++; //a flag é pra marcar o início da sequência de zero(s)
- if (flag == 1) start = head->index; //início da seq
- end = head->index; //fim da seq, que vai rodar enquanto ler zero
- }
- if (head->item == 49 || head->next == NULL) { //UM OU O FIM
- int dif = end - start; //printf("dif=%d\n", dif);
- if (dif > maior) {
- maior = dif;
- index_i = start;
- index_f = end;
- }
- flag = 0;
- }
- head = head->next;
- }
- printf ("%d %d\n", index_i, index_f);
- }
- void print_list (node *head) {
- while (head != NULL) {
- printf ("%d\n", head->item); //printf ("%d\n", head->index);
- head = head->next;
- }
- }
- node* add_end (node *head, char item, int i) {
- node *new_node = (node*) malloc (sizeof(node));
- new_node->item = item;
- new_node->next = NULL;
- new_node->index = i;
- if (head == NULL) return new_node;
- node *current = head;
- while (current->next != NULL) current = current->next;
- current->next = new_node;
- return head;
- }
- int main() {
- node *head = NULL;
- char str [10000];
- int i;
- while (1) {
- scanf (" %[^\n]s", str);
- if (strcmp(str, "0") == 0) break;
- for (i = 0; i < strlen(str); i++) {
- head = add_end (head, str[i], i); //printf ("%d\n", head);
- }
- //print_list (head);
- compare (head);
- head = NULL;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement