fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // Função para verificar se um número já existe num array
  6. int existe(int array[], int tamanho, int numero) {
  7. for (int i = 0; i < tamanho; i++) {
  8. if (array[i] == numero) {
  9. return 1; // Verdadeiro, o número já existe
  10. }
  11. }
  12. return 0; // Falso, o número não existe
  13. }
  14.  
  15. // Função para ordenar um array em ordem crescente
  16. void ordenar(int array[], int tamanho) {
  17. for (int i = 0; i < tamanho - 1; i++) {
  18. for (int j = 0; j < tamanho - i - 1; j++) {
  19. if (array[j] > array[j + 1]) {
  20. int temp = array[j];
  21. array[j] = array[j + 1];
  22. array[j + 1] = temp;
  23. }
  24. }
  25. }
  26. }
  27.  
  28. int main() {
  29. int numeros[5]; // Array para os 5 números principais
  30. int estrelas[2]; // Array para as 2 estrelas
  31. int i;
  32.  
  33. // Inicializar a semente para números aleatórios com base no tempo
  34. srand(time(NULL));
  35.  
  36. // Gerar 5 números principais únicos (1 a 50)
  37. for (i = 0; i < 5; i++) {
  38. int novo_numero;
  39. do {
  40. novo_numero = (rand() % 50) + 1; // Gera número entre 1 e 50
  41. } while (existe(numeros, i, novo_numero)); // Repete se já existir
  42. numeros[i] = novo_numero;
  43. }
  44.  
  45. // Ordenar os números principais
  46. ordenar(numeros, 5);
  47.  
  48. // Gerar 2 estrelas únicas (1 a 12)
  49. for (i = 0; i < 2; i++) {
  50. int nova_estrela;
  51. do {
  52. nova_estrela = (rand() % 12) + 1; // Gera número entre 1 e 12
  53. } while (existe(estrelas, i, nova_estrela)); // Repete se já existir
  54. estrelas[i] = nova_estrela;
  55. }
  56.  
  57. // Ordenar as estrelas
  58. ordenar(estrelas, 2);
  59.  
  60. // Mostrar a chave gerada
  61. printf("Chave do Euromilhoes:\n");
  62. printf("Numeros: ");
  63. for (i = 0; i < 5; i++) {
  64. printf("%d ", numeros[i]);
  65. }
  66. printf("\nEstrelas: %d %d\n", estrelas[0], estrelas[1]);
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5292KB
stdin
/*  Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in 	*/
/*	einer Textdatei. 													*/
/*  Die Werte müssen auf einer separaten Zeile gespeichert sein			*/
/* 																		*/
/*	Erstellt: 17.5.2010													*/
/*  Autor: Thomas Scheffler												*/

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE 32

unsigned Hamdist(unsigned x, unsigned y)
{
  unsigned dist = 0, val = x ^ y;
 
  // Count the number of set bits
  while(val)
  {
    ++dist; 
    val &= val - 1;
  }
 
  return dist;
}



int main (void)
{
	char hex;
	int i;
	int a[ARRAY_SIZE];
	int b[ARRAY_SIZE];
	int hamDist = 0;
	FILE* fp;
	
	//Arrays mit 0 initialisieren
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
  		a[i] = 0;
  		b[i] = 0;
	}

	
	fp = fopen("hex.txt","r");
	if (fp == NULL) 
	{
		printf("Die Datei hex.txt wurde nicht gefunden!");
		exit(EXIT_FAILURE);
	}

	i=0;
	printf("1.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
        a[i]=strtol(&hex,0,16);
		i++;
    }
	i=0;
	printf("2.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
    	b[i]=strtol(&hex,0,16);
        i++;
    }
	fclose(fp);

	printf("Hamming-Abweichung pro Nibble:\n");
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
		printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
		hamDist += Hamdist(a[i],b[i]);
	}
	printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}

stdout
Chave do Euromilhoes:
Numeros: 2 5 9 21 39 
Estrelas: 4 9