백준 알고리즘 / 5.step1(2)

dudgus5766·2021년 7월 20일
0

알고리즘

목록 보기
7/15
post-thumbnail

3052번

문제 : https://www.acmicpc.net/problem/3052

풀이

/*
문제 : 두 자연수 A와 B가 있을 때, A%B는 A를 B로 나눈 나머지 이다. 예를 들어, 7, 14, 27, 38을 3으로 나눈 나머지는 1, 2, 0, 2이다. 
수 10개를 입력받은 뒤, 이를 42로 나눈 나머지를 구한다. 그 다음 서로 다른 값이 몇 개 있는지 출력하는 프로그램을 작성하시오.
입력 : 첫째 줄부터 열번째 줄 까지 숫자가 한 줄에 하나씩 주어진다. 이 숫자는 1,000보다 작거나 같고, 음이 아닌 정수이다.
출력 : 첫째 줄에, 42로 나누었을 때, 서로 다른 나머지가 몇 개 있는지 출력한다.
*/

// 답안

let fs = require('fs');
let inputs = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
// trim()을 해줘야 마지막 개행을 제거해 정답을 만든다! 'trim 습관화!!'

let input = inputs.map(x => x%42);
//42로 나눈 나머지들을 배열로 만들고

let rest = new Set(input);
//set을 사용하여 중복된 배열을 제거해주는 set 오브젝트를 만든다.

let result = [...rest];
//다시 [...set]을 통해 배열로 전환(배열의 수를 읽어야하기 때문에)

console.log(result.length);

5337번

문제 : https://www.acmicpc.net/problem/5337

풀이

/*
문제 : Welcome을 예제 출력처럼 출력하는 프로그램을 작성하시오.
출력 : Welcome을 아래 예제 출력처럼 출력한다.
*/

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');

console.log('.  .   .');
console.log('|  | _ | _. _ ._ _  _');
console.log('|/\\|(/.|(_.(_)[ | )(/.');

6749번

문제 : https://www.acmicpc.net/problem/6749

풀이

/*
문제 : You know a family with three children. Their ages form an arithmetic sequence: the difference in ages between the middle child and youngest child is the same as the difference in ages between the oldest child and the middle child. For example, their ages could be 5, 10 and 15, since both adjacent pairs have a difference of 5 years.
Given the ages of the youngest and middle children, what is the age of the oldest child?
입력 : The input consists of two integers, each on a separate line. The first line is the age Y of the youngest child (0 ≤ Y ≤ 50). The second line is the age M of the middle child (Y ≤ M ≤ 50).
출력 : The output will be the age of the oldest child.
*/

// 답안

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');

let b = Number(input[1]);
let c = Number(input[0]);

function result(b,c){
    return 2 * b - c;
}

console.log(result(b,c));

6778번

문제 : https://www.acmicpc.net/problem/6778

풀이

/*
문제 : Canada Cosmos Control has received a report of another incident. They believe that an alien has illegally entered our space. A person who witnessed the appearance of the alien has come forward to describe the alien’s appearance. It is your role within the CCC to determine which alien has arrived. There are only 3 alien species that we are aware of, described below:
- TroyMartian, who has at least 3 antenna and at most 4 eyes;
- VladSaturnian, who has at most 6 antenna and at least 2 eyes;
- GraemeMercurian, who has at most 2 antenna and at most 3 eyes.
입력 : The first line contain the number of antenna that the witness claimed to have seen on the alien. The second line contain the number of eyes seen on the alien.
출력 : The output will be the list of aliens who match the possible description given by the witness. If no aliens match the description, there is no output.
*/

// 답안

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split('\n');

let a = Number(input[0]);
let e = Number(input[1]);

if (a >= 3 && e <= 4) {
    console.log("TroyMartian");
}

if (a <= 6 && e >= 2) {
    console.log("VladSaturnian");
}

if (a <= 2 && e <= 3) {
    console.log("GraemeMercurian");
}

7287번

문제 : https://www.acmicpc.net/problem/7287

풀이

/*
문제 : 자신이 백준 온라인 저지(BOJ)에서 맞은 문제의 수와 아이디를 그대로 출력하는 프로그램을 작성하시오.
출력 : 첫 줄에 자신이 맞은 문제의 수, 둘째 줄에 아이디를 출력한다.
*/

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().split(' ');

console.log('17\n');
console.log('rhfovk');

// 처음에 웹 내에서 정보를 가져와서 출력하는 어려운 문제인 줄 알았는데 마이페이지에 있는 문제 갯수와 아이디만 적으면 되는 간단한 문제였다.

7891번

문제 : https://www.acmicpc.net/problem/7891

풀이

/*
문제 : Given two integers, calculate and output their sum.
입력 : The input contains several test cases. The first line contains and integer t (t ≤ 100) denoting the number of test cases. Then t tests follow, each of them consisiting of two space separated integers x and y (−109 ≤ x, y ≤ 109).
출력 : For each test case output output the sum of the corresponding integers.
*/

// 답안

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
// let inputs = '4\n-100 100\n2 3\n0 110101\n-1000000 1\n'
// let input = inputs.trim().split('\n');

let result = "";
for(let i = 1; i < input.length; i++){
    let num = input[i].split(' ').map(Number);
    result = num[0] + num[1];
    // console.log(num);
    console.log(result);
}

8370번

문제 : https://www.acmicpc.net/problem/8370

풀이

/*
문제 : Byteland Airlines recently extended their aircraft fleet with a new model of a plane. The new acquisition has n1 rows of seats in the business class and n2 rows in the economic class. In the business class each row contains k1 seats, while each row in the economic class has k2 seats.
Write a program which:
- reads information about available seats in the plane,
- calculates the sum of all seats available in that plane,
- writes the result.
입력 : In the first and only line of the standard input there are four integers n1, k1, n2 and k2 (1 ≤ n1, k1, n2, k2 ≤ 1 000), separated by single spaces.
출력 : The first and only line of the standard output should contain one integer - the total number of seats available in the plane.
*/

// 답안

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split(' ');
// let inputs = '2 5 3 20'
// let input = inputs.trim().split(' ');

console.log(input[0] * input[1] + input[2] * input[3]);

9316번

문제 : https://www.acmicpc.net/problem/9316

풀이

/*
문제 : 당신은 N개의 테스트케이스들에게 반드시 인사를 해야 이 문제를 풀 수 있다.
N개의 줄에 걸쳐
"Hello World, Judge i!"
를 출력하는 프로그램을 만들라. 여기서 i는 줄의 번호이다.
입력 : N이 주어진다. (1 ≤ N ≤ 200)
출력 : 한 줄에 하나의 Hello World, Judge i! 를 출력한다.
*/

// 답안

let fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split(' ');
let num = Number(input);
let result = '';

for(let i = 1; i <= num; i++){
    result = `Hello World, Judge ${i}!`
    console.log(result);
}

💡마치며

3052번 나머지 문제에서 '배열에서 중복을 삭제하는 방법' 은 따로 포스팅 하기로!

profile
RN App Developer

0개의 댓글