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
|
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sstream>
using namespace std;
// Q1. Fonction récursive pour trouver le prochain diviseur donc la division donne un résultat entier
int findNextDiv(int nb,int* facteurs,int iteration){
for(int i = 2; i<=nb ;i++){
if ((nb/i)*i==nb){
facteurs[iteration]=i;
return findNextDiv(nb/i,facteurs,iteration+1);;
}
}
return iteration;
}
// Q2. Fonction qui crée le tableau de facteurs distincs et de leurs exposants
int assignPower(int* factors, int* distinctFactors, int* exponents, int lastIndex){
int distinctFactorsIndex = 0;
distinctFactors[0] = factors[0];
exponents[0] = 1;
for(int i = 1 ; i < lastIndex ;i++){
if(factors[i]!=distinctFactors[distinctFactorsIndex]){
distinctFactorsIndex++;
distinctFactors[distinctFactorsIndex]=factors[i];
exponents[distinctFactorsIndex]=1;
}
else{
exponents[distinctFactorsIndex]+=1;
}
}
return distinctFactorsIndex;
}
// Q3. Conversion int -> string
string toString(int i){
ostringstream oss;
oss << i;
return oss.str();
}
// Q3. Procédure récursive créant les diviseurs à partir des tableaux de facteurs distincts et d'exposants
void recursiveDiv(int index, int* distinctFactors, int* exponents, int lastDistinctIndex, int baseDiv){
for(int i = 0 ; i <= exponents[index];i++){
int div = baseDiv;
for(int j = 0; j<i;j++){
div *= distinctFactors[index];
}
if(index<lastDistinctIndex){
recursiveDiv(index+1, distinctFactors,exponents,lastDistinctIndex, div);
}
else{
cout << div << "\n";
}
}
}
// Q3. Procédure chapeau lancant la procédure de recherche de diviseur
void findDiv(int* distinctFactors, int* exponents, int lastDistinctIndex){
recursiveDiv(0, distinctFactors, exponents, lastDistinctIndex, 1);
}
int main()
{
int a;
int* factors = new int[100];
int* distinctFactors = new int[100];
int* exponents = new int[100];
scanf("%i",&a);
int lastIndex = findNextDiv(a,factors,0);
int lastDistinctIndex = assignPower(factors, distinctFactors,exponents,lastIndex);
findDiv(distinctFactors, exponents, lastDistinctIndex);
return 0;
} |
Partager