Tp2_Kaprekar.c
1.77 KB
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
92
93
94
95
#include <stdio.h>
void decomp3(int val, int* centaine, int* dizaine,int* unite){
*centaine = val/100;
val = val - ((*centaine)*100);
*dizaine = val/10;
val = val - ((*dizaine)*10);
*unite = val;
}
int recomp3( int centaine, int dizaine,int unite){
return centaine*100+dizaine*10+unite;
}
void permuter (int* a, int* b){
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void MaxMin(int* v1,int* v2){
if(*v2>*v1){
permuter(v1,v2);
}
}
void ordonne3 (int*a, int*b, int*c){
MaxMin(a,b);
MaxMin(b,c);
MaxMin(a,b);
}
void ordonne3croissant (int*a, int*b, int*c){
MaxMin(a,b);
MaxMin(b,c);
MaxMin(a,b);
permuter(a,c);
}
int calcNbr(int val){
int centaine, dizaine, unite;
int N1, N2, NBR;
decomp3(val,¢aine,&dizaine,&unite);
ordonne3(¢aine,&dizaine,&unite);
N1=recomp3(centaine, dizaine, unite);
ordonne3croissant(¢aine,&dizaine,&unite);
N2=recomp3(centaine, dizaine, unite);
NBR=N1-N2;
return NBR;
}
int convergence(int val){
int valSave=val;
int NBR;
NBR = calcNbr(val);
while(valSave != NBR){
valSave = NBR;
NBR = calcNbr(NBR);
//printf("%d\n",NBR);
}
return NBR;
}
int main(){
int val,NBR;
scanf("%d",&val);
NBR = convergence(val);
printf("Nombre: %d Convergence: %d \n",val, NBR);
/*int N1, N2, NBR;
decomp3(val,¢aine,&dizaine,&unite);
ordonne3(¢aine,&dizaine,&unite);
N1=recomp3(centaine, dizaine, unite);
ordonne3croissant(¢aine,&dizaine,&unite);
N2=recomp3(centaine, dizaine, unite);
NBR=N1-N2;
//printf("Valeur initiale: %d \n N1: %d \n N2: %d \n NBR: %d\n",val,N1,N2,NBR);
*/
return 0;
}