- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 C++ 中将罗马数字转换为小数。
所以我的代码应该将罗马数字转换为小数,但它并不完全有效。
例如,VI为4,IV为6。
MCMXLVI 应该产生 1946,但如果我从左到右,我得到 -998,如果我从右到左,我得到 0。
我主要想知道我的思路对不对。
伪代码:
total = 0
max_value_so_far = 0
for each character in the input string, going from right to left:
if character converted to decimal >= max_value_so_far
add character converted to decimal to total
update max_value_so_far
otherwise subtract character converted to decimal from total
代码:
#include "std_lib_facilities_5.h"
string convert_string( string input){
for(int i=0; i < input.length(); i++){
input[i] = toupper(input[i]);
}
return input;
}
int roman_to_int(string RomanChars){
RomanChars = convert_string(RomanChars);
int total = 0;
int max_value= 0;
int M,D,C,L,X,V,I;
M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
V = 5;
I = 1;
double StringLength =RomanChars.length();
for( int i = 0; i < StringLength; i++){
if(RomanChars[i] == 'M') {
if (M >= max_value) {
total += M;
max_value = M;
} else {
total -= M;
}
}
if(RomanChars[i] == 'D') {
if (D >= max_value) {
total += D;
max_value = D;
} else {
total -= D;
}
}
if(RomanChars[i] == 'C') {
if (C >= max_value) {
total += C;
max_value = C;
} else {
total -= C;
}
}
if(RomanChars[i] == 'L') {
if (L >= max_value) {
total += L;
max_value = L;
} else {
total -= L;
}
}
if(RomanChars[i] == 'X') {
if (X >= max_value) {
total += X;
max_value = X;
} else {
total -= X;
}
}
if(RomanChars[i] == 'V') {
if (V >= max_value) {
total += V;
max_value = V;
} else {
total -= V;
}
}
if(RomanChars[i] == 'I') {
if (I >= max_value) {
total += I;
max_value = I;
} else {
total -= I;
}
}
}
return total;
}
int main() {
string character;
int conversion = 0;
while(cin >> character){
conversion = roman_to_int(character);
cout << conversion <<endl;
}
return 0;
}
最佳答案
你教授提供的算法是对的。
一些提示:
for( double i = 0; i < RomanChars.length(); i++){
double
?作为循环变量?不要那样做。 (不是你问题的原因,而是 baaad)另一个答案已经指出你的 else 是错误的。如果我是你,我会考虑寻找一些优雅的东西来取代这种复制和粘贴狂欢。
如果您愿意——只是为了获得灵感——将您的罗马数字文字值存储在一个 std::map 中,您的转换循环将类似于这个循环(我示例中的 std::map 是 std::map<char, int> conversion
):
int roman_to_int(std::string RomanChars){
RomanChars = convert_string(RomanChars);
int total = 0;
int max_value= 0;
for( size_t i = RomanChars.length()-1; i != std::string::npos; --i){
auto val = conversion.find(RomanChars[i]);
if(val != conversion.end())
{
if(val->second >= max_value)
{
total += val->second;
max_value = val->second;
} else {
total -= val->second;
}
}
}
return total;
}
关于c++ - 将罗马数字转换为小数c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258864/
如何将整数类型转换为 double /浮点类型以显示小数点?例如,如果我想将数字转换为货币格式: 5 会变成 5.004.3 会变成 4.30 javascript 有什么东西可以用来做这种转换吗?
【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/cnb-yuchen/p/18107586 出自【进步*于辰的博客】
我意识到这是一个重复的问题,但是 this 中提到的解决方案这个问题完全不适合我。 我目前的代码如下 Sub ConvertTextToNumber() Dim Area As Range, C As
我正在使用数学 javascript,但在用它来替换点的逗号和逗号的点时遇到了一些麻烦。我可以改变千位分隔符的逗号,但无法设法将小数点变成逗号。我尝试了其他帖子中的一些建议,但没有感到高兴。目标是实现
我正在尝试在 Android 中创建一个数字选择器,但轮子只增加 1。我想增加 0.1。我在网上查了一下,但我发现了一个格式化的浮点数组禁用了轮子。请帮助并为语法感到抱歉,我正在学习。 最佳答案 您可
我正在尝试在多个网站上获取利率。数据相当非结构化,但形式足够接近。我想要捕捉的内容: x.xx% 至 xx.xx% 数据示例: 由 FDIC 成员 WebBank 发放的所有贷款。您的实际利率取决于信
在 MySQL 表中,我有一个具有不同值的 VARCHAR 列,这些值可能代表字符串、整数、浮点、任意值。这些值作为特定于语言的字符串写入数据库,这意味着 123.45 的浮点值可以写为德语中的 "1
我想编写一个正则表达式,它允许整数或具有 0 - 2 个小数位的小数。 有效输入 1 1. 1.1 1.11 111111111 111111111. 111111111.1 111111111.11
我正在尝试为 nullable 实现客户端验证其小数点分隔符可以是逗号(例如:123,45)。 在我看来: ... @Html.LabelFor(model => model.Turnove
我找不到合适的正则表达式来仅从字符串中提取 float 。考虑以下字符串: $string = "8x2.1 3x2"; 我想提取 2.1,我尝试了以下操作,但这给了我整数和 float : preg
我希望使用正则表达式函数分离以下数据,如下所示: 要使用的功能: let fx=(text,regex)=> Web.Page( " var x='
我是 jquery 新手。我有一个带有两个输入框的表单。我实现了一些验证。 Min.Amount Max.Amount
我正在java中实现一个简单的算法,它接受一个整数数组,并查找并返回数组中相邻整数的最大乘积。 为此,我首先初始化了一个名为largestProduct的变量,我用它来跟踪当前找到的最大(最佳)产品。
在 JavaScript 中,我想定义小数点的位置。我只能在示例中真正展示它。 假设输入值为 1234 。 我希望输出为 123.4 。 或者,如果输入是 12345 ,我希望输出是 123.45 。
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我目前正在开发一些基于 Django 的 Web 项目,在这个 Web 开发过程中,我遇到了以下我无法正确理解的代码。 if price_product['price'] == Decimal('-1
这个问题在这里已经有了答案: How do I print a double value with full precision using cout? (17 个答案) 关闭 7 年前。 我试图在
这应该是微不足道的,但我正在兜圈子,也许有人可以提供帮助。 我有两个表(T1,T2),我希望从中提取每行中的多个值并更新第三个表(T3)的内容当且仅当)T1 中有两个 UQ,NN 字段,T2 匹配,在
如果数字不是十进制,我需要附加.00,但是当我尝试下面的代码时,它会将整个数字更改为0.00。例如,如果数字是12,200,它会将其更改为0.00,而不是在末尾添加.00 $('.total-amou
我正在尝试在容器 View 中设置 9:16 纵横比 View 。以下代码在 viewDidLayoutSubviews 中设置约束,以便在正确的位置考虑自动布局。它还调用 layoutIfNeede
我是一名优秀的程序员,十分优秀!