Leetcode#13:romanToInt 暑假训练第一弹
来源:互联网 发布:基金有什么软件 编辑:程序博客网 时间:2024/06/16 12:24
长时间未没有练习,生疏了太多。
语言不够简洁,慢慢来吧
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
#include <iostream>#include <cstring>#include <algorithm>using namespace std; int romanToInt(string s) { int ans = 0; int s1[20]={0}; for (int i = 0; i < s.length(); i++){ if(s[i]=='I') s1[i]=1; if(s[i]=='V') s1[i]=5; if(s[i]=='X') s1[i]=10; if(s[i]=='L') s1[i]=50; if(s[i]=='C') s1[i]=100; if(s[i]=='D') s1[i]=500; if(s[i]=='M') s1[i]=1000; else ; } if(s.length()==1) ans = ans+s1[0]; if(s.length()>1){ for (int i = 0; i < (s.length()-1); i++){ if(s1[i]<s1[i+1]) ans = ans - s1[i]; else ans = ans + s1[i]; } ans = ans + s1[(s.length()-1)]; } return ans; } int main(){ string s; cin>>s; cout <<romanToInt(s)<<endl; return 0;}
0 0
- Leetcode#13:romanToInt 暑假训练第一弹
- leetcode romanToInt
- leetcode RomanToInt
- romanToInt
- RomanToInt
- 暑假训练第一期---思维题1
- SWPU暑假第一场训练赛题解
- 2017暑假训练第一场的一些题目
- 暑假训练-训练8.5 KMP
- 暑假训练.2
- 【暑假专题训练#数据结构】
- 2015暑假训练总结
- 暑假训练赛20160719
- 暑假训练赛20160720
- 暑假集训-训练 背包
- hpu暑假训练G
- hpu暑假训练B
- hpu暑假训练C
- oracle&mysql的日志信息
- java 锁机制
- 1- mongoDB安装及基础
- 如何记忆Android模拟器中的快捷键
- 人生就是一个大项目
- Leetcode#13:romanToInt 暑假训练第一弹
- APIO2009 抢掠计划
- GDI
- How Can I Create Histogram Projection In Opencv ?
- 学习如何从源代码分析Android Sdk--LinearLayout
- programming-challenges WERTYU (110301) 题解
- grails gsp 无法热部署(2.4)
- 正则表达式如何验证邮箱
- 如何用C设计和实现抽象数据类型(封装)