Skip to content

Commit cd8e4dd

Browse files
committedMay 10, 2021
really done with array_hash, starting hash.c
1 parent d1afd38 commit cd8e4dd

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
 

‎array_hash.c

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
//https://thehuxley.com/problem/316/code-editor/?quizId=6236
6+
/*
7+
Valor = (Posição no alfabeto) + (Elemento de entrada) + (Posição do elemento)
8+
Valor = %c - 48 + [numero da linha] + posição do elemento dentro do array de numero []
9+
*/
10+
#define DEBUG if(0)
11+
int hash(char alphabet, int lineNum, int elemPos)
12+
{
13+
if (alphabet == '\n') return 0;
14+
DEBUG printf("[%c] = %d\n",alphabet,(alphabet-65) + lineNum + elemPos);
15+
return (alphabet-65) + lineNum + elemPos;
16+
}
17+
int preHash(char input[], int lineNum, int lenght)
18+
{
19+
int pass = 0;
20+
for (int i = 0; i < lenght; i++)
21+
{
22+
pass = pass + hash(input[i],lineNum,i);
23+
}
24+
DEBUG printf("line = [%d] | pass = [%d]\n",lineNum,pass);
25+
return pass;
26+
}
27+
int function(int lines) //CBA
28+
{ int pass = 0, total = 0, len = 0;
29+
char input[51];
30+
getchar();
31+
for (int i = 0; i < lines; i++)
32+
{
33+
DEBUG printf("#########################\n");
34+
gets(input);
35+
len = strlen(input);
36+
//if(input[len-1] != '\n') strcat(input,"\n");
37+
DEBUG printf("len = [%d]\n",len);
38+
DEBUG printf("LINHA %d = {%s}\n",i,input);
39+
total = preHash(input, i, len) + total;
40+
DEBUG printf("TOTAL = [%d]\n",total);
41+
}
42+
return total;
43+
}
44+
int main() {
45+
int cases, ans, lines;
46+
scanf("%d",&cases);
47+
for (int i = 0; i < cases; i++)
48+
{
49+
DEBUG printf("------------------------ CASO [%d] ---------------------\n",i+1);
50+
scanf("%d",&lines);
51+
DEBUG printf("lines = [%d]\n",lines);
52+
ans = function(lines);
53+
printf("%d\n",ans);
54+
}
55+
56+
return 0;
57+
}
58+
59+
// 5
60+
// 2
61+
// CBA
62+
// DDD
63+
// 1
64+
// Z
65+
// 6
66+
// A
67+
// B
68+
// C
69+
// D
70+
// E
71+
// F
72+
// 6
73+
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
74+
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
75+
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
76+
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
77+
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
78+
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
79+
// 1
80+
// ZZZZZZZZZZ

‎hash.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
typedef struct _node
5+
{
6+
int data;
7+
struct _node* next;
8+
}node;
9+
typedef struct _list
10+
{
11+
int size;
12+
node* head;
13+
}list;
14+
15+
int hashFunction(int x)
16+
{
17+
return x%13;
18+
}
19+
20+
int main()
21+
{
22+
int testNumber;
23+
scanf("%d",&testNumber);
24+
for (int i = 0; i < testNumber; i++)
25+
{
26+
int baseNumber;
27+
scanf("%d",&baseNumber);
28+
int keysNumber;
29+
scanf("%d",&keysNumber);
30+
get(baseNumber,keysNumber);
31+
print(table);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.