【9407】加法表

Time Limit: 10 second
Memory Limit: 2 MB

问题描述
著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字。
例如:
+  L  K  V  E  
L  L  K  V  E  
K  K  V  E  KL  
V  V  E  KL  KK  
E  E  KL  KK  KV  

其含义为:L+L=L,L+K=K,L+V=V,L+E=E,K+L=K,K+K=V,K+V=E,K+E=KL,……E+E=KV
根据这些规则可推导出:L=0,K=1,V=2,E=3
同时,可以确定该表表示的是4进制加法

Input

n(n<=9),表示行数,以下N行,每行N个字符串,每个字符串间用空格隔开。(字串仅有一个为”+”号,其他都由大写字母组成)

Output

(1)各个字母表示什么数,格式如:L=0,K=1,……
(2)加法运算是几进制的
(3)若不可能组成加法表,则应输出”ERROR!”

Sample Input

3
+ M L
M ML M
L M L

Sample Output

M=1 L=0
2

【题目链接】:

【题解】

把题目描述中那个样例写成数字

+ 0 1  2  3
0 0 1  2  3
1 1 2  3  10
2 2 3  10 11
3 3 10 11 12
可以看到把第一列和第一行划掉之后;单个字母,它的出现次数减去1就是这个字母代表的数字;
然后这个字母表的进制就是n-1(n是行数);
如果某个字母在第一行出现过,但是在剩余的n-1阶矩阵里面它的出现次数为0,就是不合法的.


【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

const int MAXN = 12;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
string s;
map <string,int> dic;
int num[MAXN];
string temp[MAXN];

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    scanf("%d",&n);
    cin >> s;
    rep1(i,1,n-1)
    {
        cin >> s;
        dic[s] = i;
        temp[i] = s;
    }
    rep1(i,1,n-1)
    {
        cin >> s;
        rep1(j,1,n-1)
            {
                cin >> s;
                int len = s.size();
                if (len==1)
                    num[dic[s]]++;
            }
    }
    rep1(i,1,n-1)
    {
        if (num[i]==0)
        {
            puts("ERROR!");
            return 0;
        }
    }
    rep1(i,1,n-1)
    {
        cout << temp[i];printf("=%d",num[i]-1);
        if (i==n-1)
            cout << endl;
        else
            putchar(' ');
    }
    cout << n-1<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626879.html