題目
描述
著名科學家盧斯爲了檢查學生對進位制的理解,他給出瞭如下的一張加法表,表中的字母代表數字。 例如:
+ 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進制加法
輸入
n(n≤9)表示行數。
以下n行,每行包括n個字符串,每個字串間用空格隔開。(字串僅有一個爲‘+’號,其它都由大寫字母組成)
輸出
① 各個字母表示什麼數,格式如:L=0,K=1,……按給出的字母順序。
② 加法運算是幾進制的。
③ 若不可能組成加法表,則應輸出“ERROR!”
輸入樣例
5
+ 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=0 K=1 V=2 E=3
4
解題思路
首先說明兩點:
1. 進制數即n-1。
2. 若可以組成加法表,那麼題中的字母一定是1~n-2
一、看到題的第一反應是搜索:
- 最暴力的搜索就是 的枚舉,算了算 ,貌似有點懸
- 加上剪枝?如果當前已確定的數字不滿足加法表就剪掉,好像挺難寫的……
二、再觀察觀察,由於 ,那麼加法表中的兩位數的前一位一定是1,而構成 的 一定是0!
既然如此,那其他的數也就可以亂搞出來了。我的方法是:找到加數爲1的那行,得到若干形如 的式子,於是連一條從a到b的邊,由於我們知道0是那個數字,最後就可以依據邊的連接關係推出所有字母……(真的是亂搞啊)
最後再檢驗檢驗,錯了就輸出 ERROR! 即可。
三、再觀察,好像還有更簡單的方法:只看加法表中的單個字母的話,該字母在加法表(不包括第一行和第一列)中出現了多少次,該字母的答案就是次數-1……記得判斷ERROR。
Code#1
#include<map>
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int n, t, d, g[15][15];
string list[15][15];
map<char, int> m;
map<char, char> e;
char zero;
inline int getNum(string s){
int res = 0;
for(int i = 0; i < s.size(); i++)
res = (res << 1) + (res << 3) + m[s[i]];
return res;
}
inline int trans(int x){
int r[15], tl = 0, res = 0;
while(x){
r[tl++] = x % n;
x /= n;
}
for(int i = tl - 1; i >= 0; i--)
res = (res << 1) + (res << 3) + r[i];
return res;
}
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
cin >> list[i][j];
if(list[i][j].size() == 2)
m[list[i][j][0]] = 1;
}
n--;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(list[i][0] == list[i][j] && list[0][j] == list[i][j])
m[list[i][j][0]] = 0, zero = list[i][j][0];
for(t = 1; t <= n; t++)
if(list[t][0].size() == 1 && m[list[t][0][0]] == 1)
break;
for(int i = 1; i <= n; i++)
if(list[t][i].size() == 1)
e[list[0][i][0]] = list[t][i][0];
char now = zero;
while(1){
m[now] = d++;
now = e[now];
if(d >= n) break;
}
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++){
if(!i && !j) continue;
g[i][j] = getNum(list[i][j]);
}
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
if(trans(g[i][0] + g[0][j]) != g[i][j])
return puts("ERROR!"), 0;
for(int i = 1; i <= n; i++)
cout << list[0][i] << "=" << g[0][i] << " ";
printf("\n%d", n);
return 0;
}
Code#2
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
int n, m[200];
string list[15][15];
inline int getNum(string s){
int res = 0;
for(int i = 0; i < s.size(); i++)
res = (res << 1) + (res << 3) + m[s[i]];
return res;
}
inline int trans(int x){
int r[15], tl, res = 0;
while(x){
r[tl++] = x % n;
x /= n;
}
for(int i = tl-1; i >= 0; i--)
res = (res << 1) + (res << 3) + r[i];
return res;
}
int main(){
memset(m, -1, sizeof(m));
scanf("%d", &n);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
cin >> list[i][j];
if(list[i][j].size() == 1 && i != 0 && j != 0)
m[list[i][j][0]]++;
}
n--;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i != 0 && j != 0)
if(trans(m[list[i][0][0]] + m[list[0][j][0]]) != getNum(list[i][j]))
return puts("ERROR!"), 0;
for(int i = 1; i <= n; i++)
printf("%c=%d ", list[0][i][0], m[list[0][i][0]]);
printf("\n%d", n);
return 0;
}