Apuntadores
Publicado por Erick (1 intervención) el 13/08/2017 02:28:57
ALGUIEN PORFAVOR QUE ME PUEDA EXPLICAR LOS PASOS E ESTE CODIGO!? SE QUE USA APUNTADORES Y ES PARA SACAR EL MINIMO COMÚN MÚLTIPLO MÁS SIN EMBARGO NO ENTIENDO LA LOGICA QUE USA Y SU MATEMÁTICA
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
bool isPrimo(int numero);
int nextPrimo(int start);
int maxArr(int *arr, int n);
int dividirArr(int *arr, int n, int numero);
int printArr(int *arr, int n);
int mcm(int *arr, int n);
bool esDivisibleArr(int *arr, int n, int numero);
int main(){
std::cout << "MCM:";
std::cout << "n = ";
int n;
std::cin >> n;
int *enteros = new int[n];
for(int i = 0; i < n; i++){
std::cin >> enteros[i];
}
std::cout << "mcm = " << mcm(enteros,n) << std::endl;
return 0;
}
int mcm(int *arr, int n){
int primo = 1;
int intMcm = 1;
while( maxArr(arr,n) > 1){
primo = nextPrimo(primo);
while( esDivisibleArr(arr,n,primo)){
dividirArr(arr,n,primo);
intMcm *= primo;
}
}
return intMcm;
}
bool esDivisibleArr(int *arr, int n, int numero){
bool divisible = false;
for(int i = 0; i < n && !divisible; i++){
if( arr[i]%numero == 0)
divisible = true;
}
return divisible;
}
int printArr(int *arr, int n){
for(int i = 0; i < n; i++){
std::cout << arr[i] <<" ,";
}
}
int dividirArr(int *arr, int n, int numero){
for(int i = 0; i < n; i++){
if( arr[i]%numero == 0)
arr[i] = arr[i]/numero;
}
}
int maxArr(int *arr, int n){
int max = -1;
if( n > 1 ){
max = arr[0];
for(int i = 1; i < n; i++){
if ( max < arr[i] )
max = arr[i];
}
}
return max;
}
int nextPrimo(int start){
start++;
while( !isPrimo(start) ){
start++;
}
return start;
}
bool isPrimo(int numero){
int nveces = 0;
for(int i = 2; i < numero && nveces == 0; i++){
if( numero%i == 0){
nveces++;
}
}
return nveces==0;
}
Valora esta pregunta
0