안녕하세요. 오늘은 단어를 만들 거예요.

문제

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

아이디어

숫자 하나와 단어 하나를 매칭시킵시다.
영어 알파벳은 26가지 종류가 있으므로 26진법 비슷하게 풀면 됩니다.
%와 /를 적절히 사용해서 B개의 단어를 만들면 됩니다.

소스코드

#include <iostream>
#include <string>
#define ll long long
using namespace std;

string change(ll x)
{
    string res = "";
    while (x)
    {
        res += x % 26 + 'A';
        x /= 26;
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    ll A, B, i;
    cin >> A >> B;
    for (i = 1; i <= B; i++)
        cout << change(i) << ' ';
}


감사합니다.

0개의 댓글