HDU 2352 Verdis Quo (map 水)

解析罗马数字的C++代码实现
本文提供了一个使用C++实现的解析罗马数字的代码示例,通过映射符号到其对应的数值,并处理相邻符号的加减运算,实现对输入罗马数字字符串的有效解析。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map <char,int> a;
int main()
{
    a['I']=1;
    a['V']=5;
    a['X']=10;
    a['L']=50;
    a['C']=100;
    a['D']=500;
    a['M']=1000;
    int t,len;
    int i;
    char str[100];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        len=strlen(str);
        int ans=0;
        for(i=0;i<len;i++)
        {
            if(str[i]!='I'&&str[i]!='X'&&str[i]!='C')
            {
                ans+=a[str[i]];
            }
            else
            {
                int j=i;
                while(j<len&&str[i]==str[j]) j++;
                int temp=a[str[i]]*(j-i);
                if(j<len&&a[str[j]]>a[str[j-1]]) ans-=temp;
                else ans+=temp;
                i=j-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/sola1994/p/4298366.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值