小white刷题记——LeetCodeHot100_12

读题心路历程: 这道题,我看完第一遍,觉得暴力来一波,把数字对应罗马字符比较特殊的提前记录下来。但是看了题解感觉还是题解的方式好一点。题解原链接:https://leetcode.cn/problems/integer-to-roman/solution/tan-xin-ha-xi-biao-tu-jie-by-ml-zimingmeng/

        利用贪心算法,输入整数之后,就找每一个数字的最大值。例如1994:

(1)因为1994>1000,所以先让1994被1000代表的罗马数字表示,记为:M,然后1994-1000变为994;

(2)994>900,所以让994被900表示,记为CM,然后994-900=94;

        ......同理,每次都用最大的数字代表的罗马字符表示。

        最后就可以实现"MCMXCIV"

具体代码:

string intToRoman(int num) {
//定义int类型和string类型的数组,用来存放提前量
	    int nums[] = { 1000,900,500,400,100,90,50,40,10,9,5,4,1 };
	    string strs[] = { "M","CM","D" ,"CD" ,"C" ,"XC" ,"L" ,"XL" ,"X" ,"IX" ,"V" ,"IV" ,"I" };
	    string res;//定义一个放最后答案的string
	    for (int i = 0; i < 13; i++)//遍历数组,从0-12
	    {
//当这个num大于某一个num[i]的时候,就进行运算,小于之后,就进行下一个for循环。
	    	while (num >= nums[i])
	    	{
		    	num = num - nums[i];
		    	res = res + strs[i];
	    	}

	    }
    	return res;
}

“ # 设置按钮的背景颜色 self.m_button1.SetBackgroundColour('#0a74f7') self.m_button1.SetForegroundColour('white') self.m_button2.SetBackgroundColour('#0a74f7') self.m_button2.SetForegroundColour('white') self.m_button3.SetBackgroundColour('#0a74f7') self.m_button3.SetForegroundColour('white') self.m_button4.SetBackgroundColour('#238E23') self.m_button4.SetForegroundColour('white') self.m_button5.SetBackgroundColour('#238E23') self.m_button5.SetForegroundColour('white') self.m_button6.SetBackgroundColour('#238E23') self.m_button6.SetForegroundColour('white') self.m_button7.SetBackgroundColour('#6F4242') self.m_button7.SetForegroundColour('white') self.m_button8.SetBackgroundColour('#6F4242') self.m_button8.SetForegroundColour('white') self.m_button9.SetBackgroundColour('#6F4242') self.m_button9.SetForegroundColour('white') self.m_button10.SetBackgroundColour('#8E6B23') self.m_button10.SetForegroundColour('white') self.m_button11.SetBackgroundColour('#8E6B23') self.m_button11.SetForegroundColour('white') self.m_button12.SetBackgroundColour('#8E6B23') self.m_button12.SetForegroundColour('white') self.m_button13.SetBackgroundColour('#8E6B23') self.m_button13.SetForegroundColour('white') self.m_button14.SetBackgroundColour('#545454') self.m_button14.SetForegroundColour('white') self.m_button15.SetBackgroundColour('#545454') self.m_button15.SetForegroundColour('white') self.m_button16.SetBackgroundColour('#545454') self.m_button16.SetForegroundColour('white') self.m_panel1.SetBackgroundColour('white') # 设置面板的背景颜色”逐行解释代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值