Rapport cryptarithm 1.0
Dev-linux
code.c
Go to the documentation of this file.
1
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13
19#define MAX_LETTRES 10
20
26#define MAX_MOTS 5
27
33#define MAX_LONGUEUR 100
34
48int verifier_solution(char mots[MAX_MOTS][MAX_LONGUEUR], int N, char totalStr[MAX_LONGUEUR], int chiffres[], char lettres[], int nb_lettres) {
49 int valeurs[N + 1];
50
51 for (int i = 0; i < N + 1; i++) {
52 valeurs[i] = 0;
53 char *mot = (i < N) ? mots[i] : totalStr;
54 for (int j = 0; j < strlen(mot); j++) {
55 int index = strchr(lettres, mot[j]) - lettres;
56 valeurs[i] = valeurs[i] * 10 + chiffres[index];
57 }
58 }
59
60 int somme = 0;
61 for (int i = 0; i < N; i++) {
62 somme += valeurs[i];
63 }
64
65 return (somme == valeurs[N]);
66}
67
81void permuter(int chiffres[], int index, int utilisé[], char mots[MAX_MOTS][MAX_LONGUEUR], int N, char totalStr[MAX_LONGUEUR], char lettres[], int nb_lettres) {
82 if (index == nb_lettres) {
83 if (verifier_solution(mots, N, totalStr, chiffres, lettres, nb_lettres)) {
84 for (int i = 0; i < nb_lettres; i++) {
85 printf("%c %d\n", lettres[i], chiffres[i]);
86 }
87 }
88 return;
89 }
90
91 for (int i = 0; i < 10; i++) {
92 if (!utilisé[i]) {
93 int est_premiere_lettre = 0;
94 for (int j = 0; j < N + 1; j++) {
95 char *mot = (j < N) ? mots[j] : totalStr;
96 if (lettres[index] == mot[0]) {
97 est_premiere_lettre = 1;
98 break;
99 }
100 }
101
102 if (est_premiere_lettre && i == 0)
103 continue;
104
105 chiffres[index] = i;
106 utilisé[i] = 1;
107 permuter(chiffres, index + 1, utilisé, mots, N, totalStr, lettres, nb_lettres);
108 utilisé[i] = 0;
109 }
110 }
111}
112
113int main() {
114 int N;
115 scanf("%d", &N);
116
117 char mots[MAX_MOTS][MAX_LONGUEUR];
118 for (int i = 0; i < N; i++) {
119 scanf("%s", mots[i]);
120 }
121
122 char totalStr[MAX_LONGUEUR];
123 scanf("%s", totalStr);
124
125 char lettres[MAX_LETTRES] = "";
126 for (int i = 0; i < N + 1; i++) {
127 char *mot = (i < N) ? mots[i] : totalStr;
128 for (int j = 0; j < strlen(mot); j++) {
129 if (!strchr(lettres, mot[j])) {
130 int len = strlen(lettres);
131 lettres[len] = mot[j];
132 lettres[len + 1] = '\0';
133 }
134 }
135 }
136
137 // Trier les lettres par ordre alphabétique
138 int nb_lettres = strlen(lettres);
139 qsort(lettres, nb_lettres, sizeof(char), (int (*)(const void *, const void *)) strcmp);
140
141 int chiffres[MAX_LETTRES], utilisé[10] = {0};
142
143 permuter(chiffres, 0, utilisé, mots, N, totalStr, lettres, nb_lettres);
144
145 return 0;
146}
#define MAX_MOTS
Definition: code.c:26
void permuter(int chiffres[], int index, int utilisé[], char mots[MAX_MOTS][MAX_LONGUEUR], int N, char totalStr[MAX_LONGUEUR], char lettres[], int nb_lettres)
génère les permutations des chiffres
Definition: code.c:81
#define MAX_LONGUEUR
Definition: code.c:33
int main()
Definition: code.c:113
int verifier_solution(char mots[MAX_MOTS][MAX_LONGUEUR], int N, char totalStr[MAX_LONGUEUR], int chiffres[], char lettres[], int nb_lettres)
vérifie si la combinaison satisfait l'addition
Definition: code.c:48
#define MAX_LETTRES
Definition: code.c:19
int N
Definition: other_code.c:14