Integer to Roman

Code:

class Solution {
public:
    string intToRoman(int num) {
        string roman;
        int nums[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        char symbols[13] = {'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V', 'I', 'I'};
        for(int i=0;num>0;i++){
            int tmp=num/nums[i];
            num=num-tmp*nums[i];
            while(tmp>0){
                roman.append(1,symbols[i]);
                if(i%2)
                    roman.append(1,symbols[i-1]);
                tmp--;
            }
        }
        return roman;
    }
};

 

posted @ 2013-10-30 07:41  WinsCoder  阅读(130)  评论(0编辑  收藏  举报