Skip to content

Commit f0317c8

Browse files
committed
working on the biggest sequence
1 parent 84a4c1e commit f0317c8

File tree

3 files changed

+183
-25
lines changed

3 files changed

+183
-25
lines changed

biggest_seq.c

+74-24
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,117 @@
1+
//https://thehuxley.com/problem/862/oracle?quizId=6096
12
#include <stdio.h>
23
#include <stdlib.h>
3-
//https://thehuxley.com/problem/261?quizId=6096
4-
#define DEBUG if(0)
4+
#include <string.h>
5+
6+
#define DEBUG if (1)
57
#define MAX 100001
68
typedef struct node
79
{
810
char item;
911
struct node *next;
10-
}NODE;
12+
} NODE;
1113

12-
NODE* create_list()
14+
NODE * create_list()
1315
{
1416
return NULL;
1517
}
1618

17-
NODE* add(NODE* head, char item[])
19+
NODE *add(NODE *head, char item)
1820
{
19-
NODE* new_node = (NODE*) malloc(sizeof(NODE));
20-
strcpy();
21-
new_node->next = head;
22-
DEBUG printf("[[[%d]]]\t",new_node->item);
21+
NODE *new_node = (NODE *)malloc(sizeof(NODE));
22+
new_node->item = item;
23+
new_node->next = head;
24+
//DEBUG printf("adicionado[[[%d]]]\t",new_node->item);
2325
return new_node;
2426
}
2527

26-
int is_empty(NODE* head)
27-
{return(head == NULL);}
28+
int is_empty(NODE *head)
29+
{
30+
return (head == NULL);
31+
}
2832

29-
void printer(NODE* head)
30-
{
31-
if(head == NULL) {printf("Lista vazia!\n");}
33+
void printer(NODE *head)
34+
{
35+
if (head == NULL)
36+
{
37+
printf("Lista vazia!\n");
38+
}
3239
while (head != NULL)
3340
{
34-
printf("[%d]",head->item);
41+
printf("[%d]", head->item);
3542
head = head->next;
3643
}
3744
printf("Fim da Lista!\n");
3845
}
3946

40-
NODE* search(NODE* head, int item)
47+
NODE *search(NODE *head, int item)
4148
{
42-
DEBUG printf("to dentro do search\n");
49+
// printf("to dentro do search\n");
4350
while (head != NULL)
4451
{
4552
if (head->item == item)
4653
{
47-
DEBUG printf("ENCONTREI MAIS UM!!! [%d]\n", item);
48-
return head;//retorna o endereço que contém o item
54+
// printf("ENCONTREI MAIS UM!!! [%d]\n", item);
55+
return head; //retorna o endereço que contém o item
4956
}
5057
head = head->next;
5158
}
5259
return NULL;
5360
}
5461

62+
int search_2(NODE *head, int item)//função para procurar a melhor sequencia
63+
{
64+
int count = 0;
65+
while (head != NULL)
66+
{
67+
if (head->item == item)
68+
{
69+
// printf("ENCONTREI MAIS UM!!! [%d]\n", item);
70+
return count; //retorna o endereço que contém o item
71+
}
72+
head = head->next;
73+
count++;
74+
}
75+
return 0;
76+
}
77+
78+
void func(NODE *head, int size, int beg, int end)
79+
{
80+
NODE* first_zero = search(head,0);//recebo o endereço do primeiro zero.
81+
beg = search_2(head,0);
82+
int size1 = search_2(first_zero,1); //a partir do endereço do primeiro zero, procuro o proximo 1 e recebo o tamanho deste intervalo
83+
end = size1
84+
if(first_zero == NULL)//break
85+
{
86+
printf("0 0\n");
87+
return;
88+
}
89+
NODE* first_one = search(first_zero, 1);
90+
}
5591
int main()
5692
{
5793
char input[MAX];
58-
int p = scanf("%s", input);
59-
NODE* lista1 = create_list(); //nó que aponta para nulo
60-
while (p != EOF)
94+
scanf("%s", input);
95+
int q = strlen(input);
96+
NODE *lista1 = create_list(); //nó que aponta para nulo
97+
98+
for (int i = 0; i < q; i++)
6199
{
62-
lista1 = add(lista1,input);
100+
lista1 = add(lista1, input[i]);
101+
63102
}
64-
103+
104+
func(lista1);
105+
do
106+
{
107+
scanf("%s", input);
108+
int q = strlen(input);
109+
110+
for (int i = 0; i < q; i++)
111+
{
112+
lista1 = add(lista1, input[i]);
113+
}
114+
} while (1);
65115

66116

67117
return 0;

teorema_valor_intermediário.c tvi.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ float func(int x)
1616

1717
float tvi(float (*f)(int), int a, int b)
1818
{
19+
int MAX_N = 1486618625;
1920
float TOL = 0.000000058;
2021
int t1 = (*f)(a);
2122
int t2 = (*f)(b);
@@ -25,7 +26,11 @@ float tvi(float (*f)(int), int a, int b)
2526
{
2627
if (mid = 0 || (b-a)/2 < TOL)
2728
{
29+
if(mid == 0)printf("mid = 0\n");
30+
if((b-a)/2 < TOL)printf("TOL\n");
31+
2832
printf("ENCONTREI!");
33+
printf("%.2f\n",mid);
2934
return mid;
3035
}
3136
if ((*f)(a)>0 && (*f)(b)>0 ||(*f)(a)<0 && (*f)(b)<0) //cria novo intervalo
@@ -37,7 +42,10 @@ float tvi(float (*f)(int), int a, int b)
3742
}
3843

3944
}
40-
45+
int main()
46+
{
47+
tvi(func,0.555, 5);
48+
}
4149
// N ← 1
4250
// while N ≤ NMAX do // limit iterations to prevent infinite loop
4351
// c ← (a + b)/2 // new midpoint

two_lists.c

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
//https://thehuxley.com/problem/261?quizId=6096
4+
#define DEBUG if(0)
5+
typedef struct node
6+
{
7+
int item;
8+
struct node *next;
9+
}NODE;
10+
11+
12+
NODE* create_list()
13+
{
14+
return NULL;
15+
}
16+
17+
NODE* add(NODE* head, int item)
18+
{
19+
NODE* new_node = (NODE*) malloc(sizeof(NODE));
20+
new_node->item = item;
21+
new_node->next = head;
22+
DEBUG printf("[[[%d]]]\t",new_node->item);
23+
return new_node;
24+
}
25+
26+
int is_empty(NODE* head)
27+
{return(head == NULL);}
28+
29+
void printer(NODE* head)
30+
{
31+
if(head == NULL) {printf("Lista vazia!\n");}
32+
while (head != NULL)
33+
{
34+
printf("[%d]",head->item);
35+
head = head->next;
36+
}
37+
printf("Fim da Lista!\n");
38+
}
39+
40+
41+
NODE* search(NODE* head, int item)
42+
{
43+
DEBUG printf("to dentro do search\n");
44+
while (head != NULL)
45+
{
46+
if (head->item == item)
47+
{
48+
DEBUG printf("ENCONTREI MAIS UM!!! [%d]\n", item);
49+
return head;//retorna o endereço que contém o item
50+
}
51+
head = head->next;
52+
}
53+
return NULL;
54+
}
55+
int belongs(NODE* head1, NODE* head2, int size1, int size2)
56+
{
57+
int reps = 0;
58+
DEBUG printf("vou começar o while\n");
59+
DEBUG printf("%p\n",(void*)&head2);
60+
while (head2 != NULL)
61+
{
62+
DEBUG printf("[%d]\n", head2->item);
63+
NODE* temp = search(head1,head2->item);
64+
DEBUG printf("ainda to no while\n");
65+
if (temp!=NULL) reps++;
66+
head2 = head2->next;
67+
}
68+
DEBUG printf("acabou o while, vou ver se encontrei a quantidade certa de repetições\n");
69+
if(reps == size2)
70+
{
71+
return 1;
72+
}
73+
return 0;
74+
}
75+
76+
int main()
77+
{
78+
int size1,size2;
79+
scanf("%d", &size1);
80+
NODE* lista1 = create_list(); //nó que aponta para nulo
81+
NODE* lista2 = create_list(); //nó que aponta para nulo
82+
for (int i = 0; i < size1; i++)
83+
{
84+
int n;
85+
scanf("%d", &n);
86+
lista1 = add(lista1,n); //fazemos lista1 receber o retorno de add(lista1,n), quando ele recebe esse valor, atualizamos a cabeça da lista!
87+
}
88+
scanf("%d",&size2);
89+
for (int i = 0; i < size2; i++)
90+
{
91+
int n;
92+
scanf("%d", &n);
93+
lista2 = add(lista2,n);
94+
}
95+
//printer(lista2);
96+
DEBUG {printf("Agora que peguei os números, vou fazer a comparação:\n");}
97+
int ans = belongs(lista1,lista2,size1,size2);
98+
printf("%d\n",ans);
99+
return 0;
100+
}

0 commit comments

Comments
 (0)