알고리즘/프로그래머스
[2018 KAKAO BLIND RECRUITMENT 3차] n진수 게임
동 코
2020. 5. 1. 22:37
문제풀이
n진수숫자를 주르륵 나열한뒤 해당하는 턴의 1글자를 계속 저장해 나가면 되는 문제이다.
n진수의 숫자를 구하는 식은 10진수의 수를 n으로 계속 나눠 몫이 0일때까지 나머지를 앞에다 계속 추가해 주는 방식이다. 초등학교 때 배운 2진수 구하는 식을 응용하면 만들 수 있다.
string nNumber(int num, int n){
string result="";
while(num/n!=0){
int remainder = num%n;
result = number[remainder]+result;
num = num/n;
}
result = number[num%n]+result;
return result;
}
그리고 숫자를 얼마까지 구해놔야될지 고민하다가 m*t 개수만큼 구했다. 사실 이거보다 적게 잡아도 되겠지만 input도 그리 크지 않아 넉넉하게 잡아도 될거 같아 그렇게 구했다.
소스코드
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string number="0123456789ABCDEF";
string nNumber(int num, int n){
string result="";
while(num/n!=0){
int remainder = num%n;
result = number[remainder]+result;
num = num/n;
}
result = number[num%n]+result;
return result;
}
string solution(int n, int t, int m, int p) {
string answer = "";
string totalStr = "";
for(int i=0;i<t*m;i++){
totalStr += nNumber(i, n);
}
for(int i=p-1;answer.size()<t;i=i+m){
answer += totalStr[i];
}
return answer;
}
int main(){
int n=16;
int t=16;
int m=2;
int p=1;
cout<<solution(n,t,m,p)<<endl;
}
|
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/17687
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr