Verdis Quo HDU2352

2 篇文章 0 订阅

在(代码一)字符串转枚举类型是,char应设为char c[], 而不是char * c;不然会RE(可能是数组越界)。然后  感觉测试样例有个BUG,就是IIV应该是3,然而代码二(好早以前写的)直接if完,IIV是5,但是AC了。这次主要是想练习练习枚举。然后 刚开始还有个想法就是用stringstream 直接将char转化为NUM(枚举),然而,编译不过去,需要自己编写>>重载....所以就自己写个函数出来了,不知道还有没有更好的方法来实现字符串转枚举类型。(网上查到了C#里面有直接转的System.Enum.Parse(type, char))

 

//代码一(AC)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include<sstream>

using namespace std;

typedef enum NUM{I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 }NUM;
//char a[7] = {I, V, X, L, C, D, M};

NUM tmp, tmp1;

NUM CtoNUM(char t)
{
    char c[1];
    c[0] = t;
    c[1] = '\0';
    if(strcmp("I", c) == 0){
        return I;
    }
    if(strcmp("V", c) == 0){
        return V;
    }
    if(strcmp("X", c) == 0){
        return X;
    }
    if(strcmp("L", c) == 0){
        return L;
    }
    if(strcmp("C", c) == 0){
        return C;
    }
    if(strcmp("D", c) == 0){
        return D;
    }
    if(strcmp("M", c) == 0){
        return M;
    }

}

int main()
{

    int n;

    cin >> n;
    //   getchar();

    while(n--) {
        char c;
        cin >> c;
        tmp = CtoNUM(c);
/*
        stringstream in;

        in << c;
        in >> (NUM)tmp;*/

        //      cout <<tmp << endl;

        int sum = 0, cnt = 1;
        while (c = getchar()) {
            if (c == '\n') {
                break;
            }
            tmp1 = CtoNUM(c);
//            cout <<tmp1 << endl;
            if (tmp == tmp1) {
  //              cout << tmp;
                if (tmp == I || tmp == X || tmp == C || tmp == M) {
  //                  cout << tmp << endl;
                    cnt++;
  //                  cout << cnt << endl;
                    continue;
                }
            }
            else if (tmp > tmp1) {
                sum += cnt * tmp;
            }
            else if (tmp < tmp1) {
                sum -= cnt * tmp;
            }
            tmp = tmp1;
            cnt = 1;
        }

        /*
        //这种IIV是5,然后这个也WA了知道有问题但是感觉比代码二好,想知道下除了前缀相同会出错还有什么会出错,毕竟代码二也有一部分相同的问题都能A了
        if(tmp >= tmp1){
            sum += cnt*tmp;
        }
        else if(tmp < tmp1){
            sum -= cnt*tmp;
        }
        tmp = tmp1;
    }*/

        cout << sum+tmp*cnt << endl;
//cout << endl << endl;
    }
    return 0;
}

代码二(有bug但是AC了)

#include <stdio.h>
#include <string.h>
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int sum=0;
        char a[50];
        scanf("%s",&a);
        int l=strlen(a);
        int i;
        for(i=0;i<l;i++)
        {
            if(a[i]=='M')
                sum+=1000;
            else if(a[i]=='D')
                sum+=500;
            else if(a[i]=='C')
            {
                if(a[i+1]=='M' ||a[i+1]=='D')
                    sum-=100;
                else
                    sum+=100;
            }
            else if(a[i]=='L')
                sum+=50;
            else if(a[i]=='X')
            {
                if(a[i+1]=='L' ||a[i+1]=='C')
                    sum-=10;
                else
                    sum+=10;
            }
            else if(a[i]=='V')
                sum+=5;
            else if(a[i]=='I')
            {
                if(a[i+1]=='V' ||a[i+1]=='X')
                    sum-=1;
                else
                    sum+=1;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值