罗马数字转换为int

来源:互联网 发布:网络分销系统发展趋势 编辑:程序博客网 时间:2024/05/26 05:53

方法一


罗马数字

罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)

一个罗马数字重复几次,就表示这个数的几倍。但同一数码不能出现三次以上。

遵循右加左减的规则。

左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
但是,左减时不可跨越一个位数。比如,99不可以用IC(100 - 1)表示,而是用XCIX([100 - 10] + [10 - 1])表示。(等同于阿拉伯数字每位数字分别表示。)
左减数字必须为一位,比如8写成VIII,而非IIX。

处理方法

把输入的罗马数字分段处理,分段相加。从左到右,一个个检测过去。

设置 当前位current,上一位pre,分段值temp。

如果当前位对应的值和上一位的一样(current == pre),那么分段值加上当前值{temp += current;}。比如III = 3

如果当前位对应的值大于上一位的(current > pre),说明分段值应该是当前值减去现有的分段值{temp = current - temp;}。比如IIV = 5 – 2

如果当前位对应的值小于上一位的(current < pre),那么可以先将当前分段值加到结果中,重新开始记录分段值{result += temp;temp = current;}。比如XI = 10 + 1

代码片段

[java] view plain copy
  1. public class NineNine {  
  2.     public static int romanToInt(String s) {  
  3.         if (s.length() < 1return 0;  
  4.         int result = 0;  
  5.         int current = 0;  
  6.         int pre = singleRomanToInt(s.charAt(0));  
  7.         int temp = pre;   
  8.         for (int i = 1; i < s.length(); i++) {  
  9.             current = singleRomanToInt(s.charAt(i));  
  10.             if (current == pre)  
  11.                 temp += current;  
  12.             else if(current > pre){  
  13.                 temp = current - temp;  
  14.             }  
  15.             else if (current < pre){  
  16.                 result += temp;  
  17.                 temp = current;  
  18.             }  
  19.             pre = current;  
  20.         }  
  21.         result += temp;  
  22.         return result;  
  23.           
  24.     }  
  25.       
  26.     public static int singleRomanToInt(char c){  
  27.         switch (c) {  
  28.         case 'I':  
  29.             return 1;  
  30.         case 'V':  
  31.             return 5;  
  32.         case 'X':  
  33.             return 10;  
  34.         case 'L':  
  35.             return 50;  
  36.         case 'C':  
  37.             return 100;  
  38.         case 'D':  
  39.             return 500;  
  40.         case 'M':  
  41.             return 1000;  
  42.         default:  
  43.             return 0;  
  44.         }  
  45.     }  
  46.     public static void main(String args[]){  
  47.         int i = romanToInt("MMMCMXCIX");  
  48.         System.out.println(i);  
  49.     }  
  50. }  
方法二
给定一个罗马数字s,( I<=s<=MMMCMXCIX)(即1到3999),将罗马数字转换成整数。
如罗马数字I,II,III,IV,V分别代表数字1, 2, 3, 4, 5。
格式:
   第一行输入一个罗马数字,接下来输出对应的整数。
提示:
   首先要来了解一下罗马数字表示法,基本字符有7个:I,V,X,L,C,D,M,分别表示1,5,10,50,100,500,1000。
在构成数字的时候,有下列规则:
1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
3、小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
4、正常使用时,连写的数字重复不得超过三次。
样例1
输入:
CXXIII
输出:

123

#include<stdio.h>#include<string.h>char str[10]="IVXLCDM";int num[10]={1,5,10,50,100,500,1000};int main(){    char s[101];    scanf("%s",s);    int len=strlen(s);    int cnt=0,a[20];    for(int i=0;i<len;i++)    {        int f=0,t;        if(s[i]==s[i+1]&&i!=len-1)        {            if(s[i]==s[i+2]&&i!=len-2)            {                f=2;            }            else                f=1;        }        for(int k=0;k<7;k++)            if(s[i]==str[k])        {            t=k;            break;        }        a[cnt++]=num[t]*(f+1);        i+=f;    } // -----    int sum=0;    for(int i=0;i<cnt;i++)        sum+=a[i];    for(int i=0;i<cnt-1;i++)    {        if(a[i]<a[i+1])sum-=2*a[i];    }    printf("%d\n",sum);    return 0;}



0 0
原创粉丝点击