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
| import java.util.Arrays;
class TryHelloWorld { public int[] gcdlcm(int a, int b) { int[] answer = new int[2]; int[] alist = new int[a]; int[] blist = new int[b]; answer[0] = 0; for(int i = 1; i <=a; i++) { if(a%i == 0 ) { alist[i-1] = i; System.out.println("alist" + alist[i-1]); } } for(int i = 1; i <=b; i++) { if(b%i == 0 ) { blist[i-1] = i; System.out.println("blist" + blist[i-1]); } } for(int i = 0; i<alist.length; i++) { Arrays.sort(blist); int idx = Arrays.binarySearch(blist, alist[i]); if(idx>=0) { if(answer[0] <= alist[i]) { answer[0] = alist[i]; } } } answer[1] = (a*b)/answer[0]; return answer; }
public static void main(String[] args) { TryHelloWorld c = new TryHelloWorld(); System.out.println(Arrays.toString(c.gcdlcm(3, 12))); } }
|