영상
문제
<script>
/*
[문제]
[1] 1~50 사이의 랜덤 숫자 중 3의 배수 3개의 합을 total에 추가한다.
[2] 위 내용을 총 다섯 번 반복한다.
[3] 합이 가장 큰 수를 변수 max에 저장하시오.
[예시]
27 27 18 = 72
30 24 15 = 69
6 12 45 = 63
45 27 48 = 120
30 18 27 = 75
max = 120
*/
let total = [];
let max = 0;
</script>
Java
복사
해설
<script>
/*
[문제]
[1] 1~50 사이의 랜덤 숫자 중 3의 배수 3개의 합을 total에 추가한다.
[2] 위 내용을 총 다섯 번 반복한다.
[3] 합이 가장 큰 수를 변수 max에 저장하시오.
[예시]
27 27 18 = 72
30 24 15 = 69
6 12 45 = 63
45 27 48 = 120
30 18 27 = 75
max = 120
*/
let total = [];
let max = 0;
for(let i=0; i<5; i++) {
let count = 0;
let sum = 0;
while(true) {
let num = Math.floor(Math.random() * 50) + 1;
if(num % 3 == 0) {
document.write(num + " ");
sum += num;
count += 1;
}
if(count == 3) {
break;
}
}
document.write(" = " + sum + "<br>");
total.push(sum);
if(max < sum) {
max = sum;
}
}
document.write("total = " + total + "<br>");
document.write("max = " + max);
</script>
Java
복사