int c;
if(当前位置是一位数){
c = 当前位置的字符串第一项的值;
}else{
c = 当前位置的字符串第一项的值*(n-1)+当前位置的字符串第二项的值;
}
if (c != 当前位置的两个加数和) {
cout << "ERROR!" << endl; //你愿意话用printf当然可以return 0;//结束,免得之后再输出
}
剩下的就都没有任何难度了。
上代码!
AC Code
C++
// 其实下面的p可以和m省略,但是m是后来偷懒写的,就没去合并了#include <bits/stdc++.h>using namespace std;
int n;
string s[15][15];
map<char, int> m;
int p[15];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cin >> s[i][j];
for (int i = 2; i <= n; ++i) {
int sum = 0;
for (int j = 1; j <= n; ++j)
if (s[i][j].size() == 2)
sum++;
p[i] = sum;
m.insert({s[i][1][0], sum});
}
for (int i = 2; i <= n; ++i)
for (int j = 2; j <= n; ++j) {
int a = p[i];
int b = p[j];
int c;
if (s[i][j].size() == 2)
c = m[s[i][j][0]] * (n-1) + m[s[i][j][1]];
else
c = m[s[i][j][0]];
if (c != a + b) {
cout << "ERROR!" << endl;
return 0;
}
}
for (int i = 2; i <= n; ++i)
cout << s[i][1] << "=" << p[i] << " ";
cout << endl << n - 1;
return 0;
}