• uva-185-暴力枚举


      请相信,这是一道水题,读了一周的题意

    题意:

    题目里面描述的那三个条件可以直接无视,关于罗马数字只要知道一个规则即可,映射如下

    I 1         V 5
    X 10      L 50
    C 100    D 500
    M 1000

    如果一个字母映射的数字比后一个字母映射的数字小,当前的数字就作为负数.

    MXMIII = 1000-10+1000+1+1+1=1993

    输入的全是合法的罗马数字,问的问题有俩个,

    在罗马数字编码中,s1+s2是否等于s3,如果相等输出Correct,要不然输出Incorrect

    在阿拉伯数字编码中s1+s2是否等于s3,

    罗马数字到阿拉伯数字编码规则,对于一个罗马数字中字母映射到0-9,当然,阿拉伯数字不能是前导0开头

    AC:30ms

    #include<stdio.h>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<memory.h>
    #include <math.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    
    string valid = "valid";
    string impossible = "impossible";
    string ambiguous = "ambiguous";
    string correct = "Correct";
    string incorrect = "Incorrect";
    int t[30];
    int total = 0;
    int vis[30];
    int val[10];
    char str[100];
    int length;
    int endLength = 0;
    char eqAfterChar;
    int sum1 = 0, sum2 = 0;
    void table()
    {
        t['I' - 'A'] = 1;
        t['X' - 'A'] = 10;
        t['C' - 'A'] = 100;
        t['M' - 'A'] = 1000;
        t['V' - 'A'] = 5;
        t['L' - 'A'] = 50;
        t['D' - 'A'] = 500;
    }
    
    void dfs(int cur, int sum)
    {
        if(total == 2)
            return;
        if(cur == length)
        {
            if(sum1+sum2 == sum)
                total++;
            return;
        }
        char c = str[cur];
        if(c == '=')
        {
            sum2 = sum;
            sum=0;
            ++cur;
            c = str[cur];
        }
        else if(c == '+')
        {
            ++cur;
            c = str[cur];
            sum1 = sum;
            sum = 0;
        }
    
        if(vis[c - 'A'] == -1)
        {
            int i = 0;
            if(c == eqAfterChar && endLength != 1)
                i = 1;
            for(; i < 10; i++)
            {
                if(val[i])
                    continue;
                val[i] = 1;
                sum = sum * 10 + i;
                vis[c - 'A'] = i;
                dfs(cur + 1, sum);
                sum = (sum-i) / 10;
                vis[c - 'A'] = -1;
                val[i] = 0;
            }
        }
        else
        {
            sum = sum * 10 + vis[c - 'A'];
            dfs(cur + 1, sum);
            sum = sum / 10 - vis[c - 'A'];
        }
    }
    
    int main(const int argc, char** argv)
    {
        freopen("d:\1.txt", "r", stdin);
        table();
        while (scanf("%s", str))
        {
            if(str[0] == '#')
                return 0;
            total = 0;
            sum1 = 0, sum2 = 0;
            memset(vis, -1, sizeof(vis));
            memset(val, 0, sizeof(val));
            length = strlen(str);
            int t1 = 0;
            int k = 1;
    
            for(int i = length - 1; i >= 0;)
            {
                if(str[i] == '=' || str[i] == '+')
                {
                    if(str[i] == '=')
                        eqAfterChar = str[i + 1];
                    k = -1;
                    --i;
                    continue;
                }
                if(i != 0 && str[i - 1] != '=' && str[i - 1] != '+')
                {
                    if(t[str[i] - 'A'] > t[str[i - 1] - 'A'])
                        t1 = t1 + (t[str[i] - 'A'] - t[str[i - 1] - 'A']) * k;
                    else
                        t1 = t1 + (t[str[i] - 'A'] + t[str[i - 1] - 'A']) * k;
    
                    --i;
                }
                else
                    t1 += t[str[i] - 'A'] * k;
                if(k == 1)
                    endLength++;
                --i;
            }
            if(t1 == 0)
                cout << correct;
            else
                cout << incorrect;
            dfs(0, 0);
            if(total == 0)
                cout << " " << impossible << endl;
            else if(total == 1)
                cout << " " << valid << endl;
            else
                cout << " " << ambiguous << endl;
        }
        return 0;
    }
  • 相关阅读:
    使用ab进行页面的压力测试
    apache http server2.2 + tomcat5.5 性能调优
    php Try Catch多层级异常测试
    用flask实现的添加后保留原url搜索条件
    会议室预定设计
    day4
    day3
    day2
    day1
    redis介绍以及安装
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/7895990.html
Copyright © 2020-2023  润新知