|
Source : Ahmed Fessi Ecrire un programme qui convertit un nombre d'une base donnée en une autre. Formattage de l'entrée : La première ligne de l'entrée contiendra trois entiers: B1, B2, et L, respectivement la base de départ, la base d'arrivée et la taille du nombre (en nombre de chiffres) dans La seconde ligne contient l'entier N à convertir, dans sa base de départ, sous la forme de L entiers représentant chacun un chiffre. Exmple : Entrée : 10 2 1 9 Sortie : 1 0 0 1 Correction Version C #include <stdio.h> #define MAX_N 64
int main() { int B1, B2, C, c, chiffre, chiffres[MAX_N]; unsigned long long N = 0, puissance = 1ULL; scanf("%d%d%d", &B1, &B2, &C); for(c = 1 ; c < C ; c++) puissance *= B1; for(c = 0 ; c < C ; c++) { scanf("%d", &chiffre); N += puissance * chiffre; puissance /= B1; } c = 0; while(N) { chiffres[c++] = N % B2; N /= B2; } if(!c) printf("0\n"); else { for(c-- ; c > 0 ; c--) printf("%d ", chiffres[c]); printf("%d\n", chiffres[0]); } return 0; } Version Pascal par Ahmed Fessi (Compilateur FreePascal) program changement_de_base;
Function Puissance(base:qword; exponent:qword):qword; var p:qword; i:longint; begin p:=1; For i:=1 to exponent do p:=p*base; Puissance:=p; end;
Function To_Decimal (b,c,n:integer):qword; begin To_Decimal:=n*puissance(b,c); end;
Function From_Decimal (b:qword; n:qword):ansistring; var ch:string[10]; s:ansistring; nn:qword; begin repeat nn:=n mod b; str(nn,ch); ch:=concat(ch,' '); Insert(ch,s,0); n:=n div b; until n div b = 0; str(n mod b,ch); Insert(ch+' ',s,0); From_Decimal:=s; end;
var i:longint; sum,b2:qword; b1,l,x:integer;
Begin readln(b1,b2,l); sum:=0; For i:=l-1 downto 0 do begin read(x); inc(sum,To_Decimal(b1,i,x)); end; if(x=0) and (l=1) then writeln(x) else writeln(From_Decimal(b2,sum)); end.
|