Rapport cryptarithm 1.0
Dev-linux
other_code.c
Go to the documentation of this file.
1
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13
15char word[6][11], letter[11], *p, line[6][11];
16
25int cmp(const void *a, const void *b){ return *(char*)a - *(char*)b; }
26
35int is_initial(char c){
36 for(int i = 0; i <= N; i++)
37 if(word[i][0] == c) return 1;
38 return 0;
39}
40
47void test(){
48 for(int i = 0; i <= N; i++)
49 for(int j = 0; word[i][j]; j++)
50 for(int k = 0;; k++)
51 if(word[i][j] == letter[k]){
52 line[i][j] = letter_to_digit[k] + '0';
53 break;
54 }
55 int sum = 0;
56 for(int i = 0; i <= N; i++)
57 sum += atoi(line[i]) * (i == N ? -1 : 1);
58 if(sum) return;
59 for(int i = 0; i < letters; i++)
60 printf("%c %i\n", letter[i], letter_to_digit[i]);
61 exit(0);
62}
63
70void assign(int i){
72 for(; letter_to_digit[i] <= 9; letter_to_digit[i]++)
73 if(digit_used[letter_to_digit[i]] == 0){
74 if(i == letters - 1) test();
75 else
77 assign(i + 1),
79 }
80}
81
82int main(){
83 scanf("%d", &N);
84 for(int i = 0; i <= N; i++)
85 for(scanf("%s", p = word[i]); *p; p++)
86 if(strchr(letter, *p) == 0) letter[letters++] = *p;
87 qsort(letter, letters, sizeof(char), cmp);
88 assign(0);
89}
int digit_used[10]
Definition: other_code.c:14
int cmp(const void *a, const void *b)
trie les lettres alphabétiquement
Definition: other_code.c:25
char line[6][11]
Definition: other_code.c:15
int is_initial(char c)
trie les lettres alphabétiquement
Definition: other_code.c:35
int N
Definition: other_code.c:14
void assign(int i)
teste chaque combinaison avant de trouver la bonne
Definition: other_code.c:70
int letters
Definition: other_code.c:14
int letter_to_digit[10]
Definition: other_code.c:14
char letter[11]
Definition: other_code.c:15
char word[6][11]
Definition: other_code.c:15
char * p
Definition: other_code.c:15
void test()
vérifie si l'addition est correcte
Definition: other_code.c:47
int main()
Definition: other_code.c:82