최댓값

 

2566번: 최댓값

첫째 줄에 최댓값을 출력하고, 둘째 줄에 최댓값이 위치한 행 번호와 열 번호를 빈칸을 사이에 두고 차례로 출력한다. 최댓값이 두 개 이상인 경우 그 중 한 곳의 위치를 출력한다.

www.acmicpc.net

 

내 코드

const fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
let N = 0;
let M = 0;
const arr = [];
input.forEach((i) => {
    i = i.split(' ').map(a=>Number(a));
    M = i.length;
    arr.push(...i);
});

const max = Math.max(...arr);
const index = arr.indexOf(max);
console.log(max)
console.log(Math.floor(index/M) +1 , index%M +1);
728x90

https://www.acmicpc.net/problem/1371

 

1371번: 가장 많은 글자

첫째 줄부터 글의 문장이 주어진다. 글은 최대 50개의 줄로 이루어져 있고, 각 줄은 최대 50개의 글자로 이루어져 있다. 각 줄에는 공백과 알파벳 소문자만 있다. 문장에 알파벳은 적어도 하나 이

www.acmicpc.net

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

const dic = {};

input.forEach(i => {
    let arr = i.split("").filter(d => d !== ' ');
    arr.forEach((a) => {
        if(dic[a] === undefined) {
            dic[a] =1;
        } else {
            dic[a] +=1;
        }
    })
}) ;

const keys = Object.keys(dic);

let answer = [];

keys.forEach(k => {
   answer.push([k, dic[k]]);
});

answer.sort(function(a,b){
    return b[1] - a[1];
})

const max = answer[0][1];
answer = answer.filter(a => a[1] === max);

const print =[];
answer.forEach(a => {
    print.push(a[0]);
});
print.sort();

console.log(print.join(''));
728x90

https://www.acmicpc.net/problem/1427

 

1427번: 소트인사이드

첫째 줄에 정렬하려고 하는 수 N이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이다.

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim();

const arr = input.split("").map(i =>  Number(i));

arr.sort(function(a,b) {
    return b - a;
});

console.log(arr.join(""));
728x90

좌표 정렬하기

 

11650번: 좌표 정렬하기

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

www.acmicpc.net

 

내 코드

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

const N = Number(input.shift());
const dic = [];

for(let i = 0 ;i < N ; i++) {
    const [x,y] = input[i].split(" ").map(i => Number(i));
    dic.push([x,y]);
}

dic.sort(function(a,b) {
    if(a[0] < b[0]) return -1;
    else if(a[0] > b[0]) return 1;
    else {
        if(a[1] < b[1]) return -1;
        else if(a[1] > b[1]) return 1;
        else {
            return 0;
        }
    }
})

const answer = [];
dic.forEach((d) => {
    answer.push(d[0]+' '+d[1]);
})

console.log(answer.join('\n'));
728x90

+ Recent posts