Middle-题目10:12. Integer to Roman

本文介绍了一种将1到3999之间的整数转换为对应罗马数字的方法,使用Java语言实现,并通过switch-case结合递归处理各种特殊情况。

题目原文:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.

题目大意:
把1-3999的整数转换成对应的罗马数字。
题目分析:
使用switch-case加递归,考虑所有特殊情况(遇到5,10,9等数字的时候)。
源码:(language:java)

public class Solution {
    public String intToRoman(int num) {
        if(num >= 1 && num <= 9) {
            switch (num) {
                case 1:{return "I";}
                case 2:case 3:{return "I"+intToRoman(num-1);}
                case 4:case 9:{return "I"+intToRoman(num+1);}
                case 5:{return "V";}
                default:{return "V"+intToRoman(num-5);}//6,7,8
            }
        }
        else if (num >= 10 && num <= 99) {
            switch (num) {
                case 10:{return "X";}
                case 50:{return "L";}
                case 40:case 90:{return "X"+intToRoman(num+10);}
                default: {
                    if(num > 90)
                        return "X"+intToRoman(num+10);
                    else if(num > 50)
                        return "L"+intToRoman(num-50);
                    else if(num > 40 && num < 50)  //XL
                        return "X"+intToRoman(num+10);
                    else 
                        return "X"+intToRoman(num-10);
                }
            }
        }
        else if (num >= 100 && num <= 999) {
            switch (num) {
                case 100:{return "C";}
                case 500:{return "D";}
                case 400:case 900:{return "C"+intToRoman(num+100);}
                default:{
                    if(num > 900)
                        return "C"+intToRoman(num+100);
                    else if(num > 500)
                        return "D"+intToRoman(num-500);
                    else if(num > 400 && num < 500) 
                        return "C"+intToRoman(num+100);
                    else 
                        return "C"+intToRoman(num-100);
                }
            }
        }
        else { //1000~3999
            switch (num) {
                case 1000:{return "M";}
                default:{return "M"+intToRoman(num-1000);}
            }
        }       
    }
}

成绩:
11ms,beats24.85%,众数8ms,23.08%
Cmershen的碎碎念:
代码很长也很繁琐,但感觉有一些情况是可以合并的,这样应该可以节约几ms。
此外注意,因为switch-case的每个分支都是return,故省略了break,但在其他情况下写switch一定不要忘记break。

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
This is the style section, please modify and give me the full code <style> /* Custom Styles for Cart Page */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } /* 调整列宽 - 增加全选列宽度 */ .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; /* 增加宽度 */ text-align: center; vertical-align: middle; white-space: nowrap; /* 防止文字换行 */ } .cart-page-section table.cart td.product-info { width: 37%; /* 微调其他列宽度 */ vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } /* 调整剩余列宽度 */ .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; /* 三列平均分配剩余宽度 */ vertical-align: middle; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0,0,0,0.1); z-index: 1000; } .cart-footer-actions .button { padding: 10px 20px; margin-left: 10px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; } .cart-footer-actions .button:hover { background-color: #d32f2f; } #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; margin-top: 10px; transition: background-color 0.3s; } #partial-checkout:hover { background-color: #0b7dda; } .selected-summary { font-size: 16px; font-weight: bold; } .selected-summary p { margin: 0 0 10px 0; } /* Responsive styles */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } .cart-footer-actions { position: sticky; bottom: 0; } .cart-footer-actions > div { flex-direction: column; align-items: flex-start; } .cart-footer-actions .selected-summary { margin-top: 20px; text-align: left; width: 100%; } /* 移动端调整全选列 */ .cart-page-section table.cart td.product-select::before { content: "选择"; } } </style>
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值