• 规律题 CIVIC DILL MIX


    CIVIC DILL MIX

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

    Roman numerals are an ancient numbering system used extensively throughout Europe through the 13th century (where it was eventually replaced by our current positional system). Vestiges of this system still exist today on clock faces, building cornerstones, Super Bowls and Star Wars episodes. The system uses the following 7 symbols:

    Symbols I, X, C and M can be repeated as needed (though never more than three times for I, X and C), so that 3 is represented as III, 27 as XXVII and 4865 as MMMMDCCCLXV. The symbols are always written from the highest value to the lowest, but for one exception: if a lower symbol precedes a higher one, it is subtracted from the higher. Thus 4 is written not as IIII but as IV, and 900 is written as CM. 
    The rules for this subtractive behavior are the following:
    1. Only I, X and C can be subtracted.
    2. These numbers can only appear once in their subtractive versions (e.g., you can’t write 8 as IIX).
    3. Each can only come before symbols that are no larger than 10 times their value. Thus we can not write IC for 99 or XD for 490 (these would be XCIX and CDXC, respectively). Note that the first two words in this problem title are invalid Roman numerals, but the third is fine.
    Your task for this problem is simple: read in a set of Roman numeral values and output their sum as a Roman numeral.

    输入

    Input will consist of multiple test cases. Each test case starts with a positive integer n indicating the number of values to add. After this will come n values (potentially several on a line), all valid Roman numerals with whitespace only coming between values. A value of n = 0 will indicate end of input. All sums will be less than 5000.

    输出

    For each test case, output the case number and the sum, both as Roman numerals, using the format shown below. Case numbers should start at I.

    示例输入

    2
    XII MDL
    4
    I I I
    I
    0

    示例输出

    Case I: MDLXII
    Case II: IV
    View Code
    /*一开始可以按照题目要求所给的罗马数字变换成阿拉伯数字,算出和,第二步把和转换成罗马数字。通过自己写几个数可以发现当数>=90,40,4,等等可以转化成俩字母表示,而且V 和X 左边的小数字只能用Ⅰ。L 和C 左边的小数字只能用X。D 和M 左边的小数字只能用C。在数字上加一横表示这个数字的1000倍。*/
    #include<stdio.h>
    #include<string.h>
    int sw(char c)
    {
        switch(c)
        {
        case 'I':return 1;
        case 'V':return 5;
        case 'X':return 10;
        case 'L':return 50;
        case 'C':return 100;
        case 'D':return 500;
        case 'M':return 1000;
        default:return 0; 
        }
    }
    
    void printroma(int sum)
    {
        while(sum)
        {
    
        if(sum>=900)
        {
            if(sum>=1000)
            {
                while(sum>=1000)
                {
                    printf("M");
                    sum = sum-1000;
                }
                if(sum<0)
                sum+=1000;
            }
            else
            {
                printf("CM");
                sum-=900;
            }
        }
        else if(sum >= 400)
        {
            if(sum>=500)
            {
                while(sum>=500)
                {
                    printf("D");
                    sum = sum-500;
                }
                if(sum<0)
                sum+=500;
            }
            else
            {
                printf("CD");
                sum-=400;
            }
        }
        else if(sum >=90)
        {
            if(sum>=100)
            {
                while(sum>=100)
                {
                    printf("C");
                    sum = sum-100;
                }
                if(sum<0)
                sum+=100;
    
            }
            else
            {
                printf("XC");
                sum-=90;
            }
        }
        else if(sum >= 40)
        {
            if(sum>=50)
            {
                while(sum>=50)
                {
                    printf("L");
                    sum = sum-50;
                }
                if(sum<0)
                sum+=50;
            }
            else
            {
                printf("XL");
                sum-=40;
            }
        }
        else if(sum >=9)
        {
            if(sum>=10)
            {
                while(sum>=10)
                {
                    printf("X");
                    sum = sum-10;
                }
                if(sum<0)
                sum+=10;
            }
            else
            {
                printf("IX");
                sum-=9;
            }
    
        }
        else if(sum>=4)
        {
            if(sum>=5)
            {
                while(sum>=5)
                {
                    printf("V");
                    sum = sum-5;
                }
                if(sum<0)
                sum+=5;
            }
            else
            {
                printf("IV");
                sum-=4;
            }
        }
        else
            printf("I"),sum--;
    
        }
    
    
    
    
    
    }
    int main()
    {
        int n,i,j;
        int len,sum;
        char s[20];int count = 1;
        while(scanf("%d",&n)&&n)
        {
            
            sum = 0;
            for(j = 0;j < n;j++)
            {
                scanf("%s",s);
                len = strlen(s);
                for(i = 0; i<len;i++)
                {
                    if(i != len-1)
                    {
                        if(sw(s[i]) >= sw(s[i+1]))
                            sum+=sw(s[i]);
                        else
                        {
                            sum+= (sw(s[i+1]) - sw(s[i]));
                            i++;
                        }
                    }
                    else
                        sum+=sw(s[i]);
                }
            }
            printf("Case ");printroma(count);
            printf(": ");
            printroma(sum);
            puts("");
            count++;
        }
        
        return 0;
    }
  • 相关阅读:
    在ASP.NET中把数据POST到其他页面
    在网页中使用javascript打开没有最大化、最小化和关闭按钮的窗口
    怎么找回被删并清空了回收站的文件
    域控制器的安装步骤二——把客户机加入到域
    升级和卸载域AD:实现域网络管理二
    不用设置BIOS,也能让光驱启动电脑!
    老版(旧版)封神榜片头,片尾曲
    域控制器的安装步骤一
    SEO面试试题
    如何将客户机添加到域?
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2450056.html
Copyright © 2020-2023  润新知