-
Notifications
You must be signed in to change notification settings - Fork 0
/
entree.c
91 lines (86 loc) · 1.52 KB
/
entree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "tools.h"
int length_permut(char const permut[]){
int tmp = strlen(permut);
int res=1;
for (int i = 0; i < tmp; ++i)
{
if (permut[i]==',')
{
res++;
}
}
return res;
}
int traduction(int permut[], const char *argv, int const taille){
int pos=0;
int pui=0;
int tmp=0;
for (int i = 0; i < taille; ++i)
{
if (argv[i]==',')
{
permut[pos]=tmp;
pos++;
pui=0;
tmp=0;
}
else {
int argv_tmp = (int)argv[i];
if (argv_tmp < 48 || argv_tmp > 57)
{
return 1;
}
tmp+=((int)argv[i]-48)*(int)pow(10.0, (double)pui);
pui++;
}
}
permut[pos]=tmp;
return 0;
}
int fact(int x){
int res = 1;
for (int i = 2; i <= x; ++i)
{
res*=i;
}
return res;
}
int verification(int const permutation[], int const taille){
int verif_produit= fact(taille);
int verif_somme= (taille*(taille+1))/2;
for (int i = 0; i < taille; ++i)
{
int permut_tmp = permutation[i];
if (permut_tmp <= 0 || permut_tmp > taille)
{
return 1;
}
verif_produit /= permut_tmp;
verif_somme -= permut_tmp;
}
return verif_somme || (verif_produit-1); // retourne 0 si tout se passe bien
}
/*int verification(int const permutation[], int const taille){
int *verif = calloc(taille, sizeof(int));
for (int i = 0; i < taille; ++i)
{
int jump = permutation[i]-1;
if (jump < 0 || jump >= taille)
{
verif[jump] == 1;
}
}
for (int i = 0; i < taille; ++i)
{
if (verif[i] == 0)
{
return 1;
}
}
free(verif);
return 0;
}*/