[자바JAVA]Between Two Sets 해커랭크
Between Two Sets
배열a와 배열b가 주어지고 a요소가 약수이면서 b요소가 배수인 숫자 X가 몇 개인 지 찾는 문제이다.
There will be two arrays of integers. Determine all integers that satisfy the following two conditions:
- The elements of the first array are all factors of the integer being considered
- The integer being considered is a factor of all elements of the second array
These numbers are referred to as being between the two arrays. Determine how many such numbers exist.
- 입출력예시1
- 입출력예시2
풀이
int flag;
: a의 요소가 current의 약수가 아니거나 b의 요소가 current의 배수가 아닌 경우 flag 1 대입 후 break -> current를 1 더해준 뒤 다시 while문 반복 실행한다.
1 | public class BetweenTwoSets { |
- test case 1번의 콘솔
이 문제는 배열a와 배열b가 주어지고 a요소가 약수이면서 b요소가 배수인 숫자 X가 몇 개인 지 찾는 문제이다.
flag가 0일때의 current값이 내가 찾던 x의 값이다.
x는 6, 12 총 2개이다. 따라서 2를 리턴한다.
1 | //test case 1번의 콘솔 |
- test case 2번의 콘솔
위의 설명과 동일하게 flag가 0일때의 current값이 내가 찾던 x의 값이다.
x는 4, 8, 16으로 총 3개이다. 따라서 3를 리턴한다.
1 | //test case 2번의 콘솔 |