예제
<script>
/*
[문제]
100 부터 500까지 다음 조건에 따라 숫자를 출력하시오.
[조건]
(1) 각 자리수에 1이 두 개 포함된 수를 한 개 출력합니다.
(2) 이어서 각 자리수에 2가 두 개 포함된 수를 두 개 출력합니다.
(3) 이어서 각 자리수에 3이 두 개 포함된 수를 세 개 출력합니다.
(4) 같은 방식으로 숫자를 하나씩 증가시키며 반복합니다.
*/
/*
[출력예시]
101 122 202 233 303 313 344 404 414 424 455 505 515 525 535 566
*/
</script>
HTML
복사
정답
<script>
/*
[문제]
100 부터 500까지 다음 조건에 따라 숫자를 출력하시오.
[조건]
(1) 각 자리수에 1이 두 개 포함된 수를 한 개 출력합니다.
(2) 이어서 각 자리수에 2가 두 개 포함된 수를 두 개 출력합니다.
(3) 이어서 각 자리수에 3이 두 개 포함된 수를 세 개 출력합니다.
(4) 같은 방식으로 숫자를 하나씩 증가시키며 반복합니다.
*/
/*
[출력예시]
101 122 202 233 303 313 344 404 414 424 455 505 515 525 535 566
*/
// [for문]
let a = 1;
let count = 0;
let count2 = 0;
let countMax = 1;
for(let i = 100; i <= 600; i++) {
let _100 = parseInt(i / 100);
let _10 = parseInt(i % 100 / 10);
let _1 = i % 10;
if(_100 == a) {
count += 1;
}
if(_10 == a) {
count += 1;
}
if(_1 == a) {
count += 1;
}
if(count == 2) {
document.write(i, " ");
count2 += 1;
if(count2 == countMax) {
count2 = 0;
a += 1;
countMax += 1;
}
}
count = 0;
}
document.write("<br>");
// [while문]
a = 1;
count = 0;
count2 = 0;
countMax = 1;
let i = 100;
while(i <= 600) {
let _100 = parseInt(i / 100);
let _10 = parseInt(i % 100 / 10);
let _1 = i % 10;
if(_100 == a) {
count += 1;
}
if(_10 == a) {
count += 1;
}
if(_1 == a) {
count += 1;
}
if(count == 2) {
document.write(i, " ");
count2 += 1;
if(count2 == countMax) {
count2 = 0;
a += 1;
countMax += 1;
}
}
count = 0;
i += 1;
}
</script>
HTML
복사


