Last active
February 20, 2017 05:06
-
-
Save davidhmaciasv/95d16234940e3cb2986b730afa151ab3 to your computer and use it in GitHub Desktop.
avance trabajo 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Commands { | |
public static long gcd(long a, long b) { | |
long t; | |
while (b != 0) { | |
t = b; | |
b = a % b; | |
a = t; | |
} | |
return a; | |
} | |
public static long lcm(long a, long b) { | |
return a * (b / gcd(a, b)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LcmGcd { | |
private static long[] arr; | |
public LcmGcd(long[] arr) { | |
this.arr = arr; | |
} | |
public static long[] sol() { | |
long min = arr[0]; | |
long max = arr[0]; | |
for (int i = 1; i < arr.length; i++) { | |
max = Commands.gcd(max, arr[i]); | |
min = Commands.lcm(min, arr[i]); | |
} | |
return new long[] { min, max }; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.StringTokenizer; | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | |
PrintWriter out=new PrintWriter(System.out); | |
out.println("Ingrese los numeros, estsos pueden ser ingresados separados por espacios o saltos de linea."); | |
out.flush(); | |
String line=""; | |
String inp=in.readLine(); | |
while(inp!=null){ | |
line+=" "+inp; | |
inp=in.readLine(); | |
} | |
StringTokenizer st=new StringTokenizer(line); | |
long[]arr=new long[st.countTokens()]; | |
for(int i=0;i<arr.length;i++){ | |
arr[i]=Long.parseLong(st.nextToken()); | |
} | |
Arrays.sort(arr); | |
LcmGcd oper=new LcmGcd(arr); | |
long[]ou=oper.sol(); | |
out.println("Maximo comun divisor: "+ou[1]); | |
out.println("Minimo comun multiplo: "+ou[0]); | |
out.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment