罗马数字

罗马数字

题解:打表,,,还犹豫了一会儿

#include<bits/stdc++.h>
#define ll long long
#define P pair<int,int>
#define pb push_back
#define lson root << 1
#define INF (int)2e9 + 7
#define maxn (int)1e5 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

int T, n;
int a[maxn];
string S[maxn];

void Inite(){
    S[20] = "XX", S[30] = "XXX", S[40] = "XL", S[50] = "L", S[60] = "LX", S[70] = "LXX", S[80] = "LXXX", S[90] = "XC";
    S[200] = "CC", S[300] = "CCC", S[400] = "CD", S[500] = "D", S[600] = "DC", S[700] = "DCC", S[800] = "DCCC", S[900] = "CM";
    S[2000] = "MM", S[3000] = "MMM", S[4000] = "MW", S[5000] = "W", S[6000] = "WM", S[7000] = "WMM", S[8000] = "WMMM", S[9000] = "MA";
    S[1] = "I", S[2] = "II", S[3] = "III", S[4] = "IV", S[5] = "V", S[6] = "VI", S[7] = "VII", S[8] = "VIII", S[9] = "IX";
    S[10] = "X";
    S[100] = "C";
    S[1000] = "M";
    S[10000] = "A";
}

void print(int x){
    int t = 0;
    int b[10];
    while(x){
        b[t++] = x % 10;
        x /= 10;
    }
    for(int i = t - 1; i >= 0; i--) {
        if(i == 5){
            for(int j = 1; j <= b[i] * 10; j++) cout << "A";
        }
        else if(i == 4){
            for(int j = 1; j <= b[i]; j++) cout << "A";
        }
        else if(i == 3){
            cout << S[1000 * b[i]];
        }
        else if(i == 2){
            cout << S[100 * b[i]];
        }
        else if(i == 1){
            cout << S[10 * b[i]];
        }
        else cout << S[b[i]];
    }
    cout << endl;
}

int main()
{       Inite();
        cin >> n;
        for(int i = 1; i <= n; i++){
            int x;
            cin >> x;
            print(x);
        }
        return 0;
}

原文链接: https://www.cnblogs.com/zgglj-com/p/9195627.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/276229

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 上午1:34
下一篇 2023年2月15日 上午1:35

相关推荐