Skip to content

Instantly share code, notes, and snippets.

@eduardojunio
Created September 4, 2016 23:41
Show Gist options
  • Save eduardojunio/feaa0e0237c0a43651b891da54038aee to your computer and use it in GitHub Desktop.
Save eduardojunio/feaa0e0237c0a43651b891da54038aee to your computer and use it in GitHub Desktop.
85 - Adivinhe The Huxley
#include <stdio.h>
void lerSenha(int tamanho, int senha[]);
void tentarChutes(int tamanho, int senha[]);
int main() {
int k, n, i;
scanf("%d", &k);
for (i = 0; i < k; i++) {
scanf("%d", &n);
int senha[n+1];
lerSenha(n, senha);
tentarChutes(n, senha);
}
return 0;
}
void lerSenha(int tamanho, int senha[]) {
int i;
for (i = 0; i < tamanho; i++) {
scanf("%1d", &senha[i]);
}
}
int excelente(int numero, int posicao, int array[]) {
if (numero == array[posicao]) {
return 1;
}
return 0;
}
int bom(int numero, int tamanho, int array[]) {
int i;
for (i = 0; i < tamanho; i++) {
if (numero == array[i]) {
return 1;
}
}
return 0;
}
int seRepeteNaArray(int numero, int posicao, int tamanho, int array[]) {
int i;
for (i = (posicao + 1); i < tamanho; i++) {
if (array[i] == numero) {
return 1;
}
}
return 0;
}
void tentarChutes(int tamanho, int senha[]) {
int chute[tamanho], e = 0, b = 0;
while (e != tamanho) {
lerSenha(tamanho, chute);
e = 0, b = 0;
int i, j = 0;
for (i = 0; i < tamanho; i++) {
j += chute[i];
}
if (j == 0) {
break;
}
for (i = 0; i < tamanho; i++) {
if (excelente(chute[i], i, senha)) {
e++;
} else if (bom(chute[i], tamanho, senha)
&& !seRepeteNaArray(chute[i], i, tamanho, chute)) {
b++;
}
}
printf("(%d,%d)\n", e, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment