zoukankan      html  css  js  c++  java
  • Integer to Roman

    class Solution {
    private:

    static int a[200];
    static map<int,string> mp;
    static vector<int> vec_list;


    void init (){
    if(a['I'])return ;

    a[0] = 0;
    a['I'] = 1;
    a['X'] = 10;
    a['C'] = 100;
    a['M'] = 1000;
    a['V'] = 5;
    a['L'] = 50;
    a['D'] = 500;

    char s[] = "IXCMVLD";

    for(int j = 0 ; s[j] ; j ++)
    {
    vec_list.push_back(a[ s[j] ]);
    mp[a[s[j]]] += s[j];
    for(int i = 0 ; s[i] != 'M' ; i ++)
    {

    if(a[ s[i] ] < a[s[j]] )
    {
    int tmp = a[ s[j] ] - a[ s[i] ] ;
    if(tmp == 49||tmp == 99||tmp == 490||tmp == 499||tmp == 990||tmp == 999)continue;
    vec_list.push_back(tmp);
    mp[ tmp ] += s[i];
    mp[ tmp ] += s[j];
    }
    }
    }
    sort(vec_list.begin(),vec_list.end());

    }

    public:
    string intToRoman(int num) {
    init();
    string n = "";

    for(int i = vec_list.size() -1 ; i >= 0 ; i --)
    {
    while(vec_list[i]<=num){
    num-=vec_list[i];
    n += mp[vec_list[i]];
    }
    }

    return n;
    }
    };
    int Solution::a[200];
    map<int,string> Solution::mp;
    vector<int> Solution::vec_list;

  • 相关阅读:
    ImageView一例
    TextView之一:子类的常用属性
    TextView之二:常用属性
    Android中的消息机制:Handler消息传递机制
    关于LayoutParams
    LinearLayout的一些注意事项
    支付宝扫描二维码登录网站
    Cok
    Cok
    STM32的USART
  • 原文地址:https://www.cnblogs.com/clover-xuqi/p/8073067.html
Copyright © 2011-2022 走看看