挑战密室,第八届河南省省赛,郑大OJ10406

来源:互联网 发布:文字特效制作软件 编辑:程序博客网 时间:2024/06/10 03:30

挑战密室,第八届河南省省赛,郑大OJ10406

哈哈,下面代码及题意分析:

string ele[]数组用来存化学元素,eleM[]用来存分子的相对分子质量,N表示元素的个数,方便拓展,增加元素个数。

元素与其相对分子质量绑定在一块,用Map(红黑树,自平衡二叉树)存放(buildBT函数实现),便于在log时间内找到元素的质量。

getAim获得化学方程式等号右边第一个化学式。

getWeight()函数获得一个化学式的相对分子质量。难点也在这里。看代码如何实现。


#include <iostream>#include <cstdio>#include <algorithm>#include <map>#include <cctype>#include <cstring>using namespace std;#define N 10//N为化学元素个数 string ele[] = { "N", "C", "O", "Cl", "S","H", "Al", "Ca", "Zn", "Na" };int eleM[] = { 14, 12, 16, 35, 32, 2, 27, 40, 65, 23 };map<string, int> Map;void buildBT(string ele[], int eleM[]) {Map.clear();for (int i = 0; i < N; ++i)Map.insert(pair<string, int>(ele[i], eleM[i]));}string getAim(string str) {int start = str.find('=') + 1, last = (int)str.find('+', start); //从start下标开始查找 if (last == -1)last = str.length();string ret;for (int i = start; i < last; ++i)ret.append(1, str[i]);return ret;}int getWeight(string str) {bool leftP = false;int tmp = 0, sum = 0;for (string::iterator it = str.begin(); it != str.end();) {if (*it == '(') {leftP = true;++it;}else {if (*it == ')') {++it;int n = 0;while (it != str.end() && isdigit(*it))n = n * 10 + *(it++) - '0';//同时it后移 n = (n == 0 ? 1 : n);sum += n * tmp;leftP = false;tmp = 0;}else if (isalpha(*it)) {string s = string(1, *it);++it;if (it != str.end() && 'a' <= *it && *it <= 'z')s.append(1, *(it++));int n = 0;while (it != str.end() && isdigit(*it))n = n * 10 + *(it++) - '0';n = (n == 0 ? 1 : n);tmp += Map[s] * n;if (!leftP) {sum += tmp;tmp = 0;}}}}return sum;}int main(){buildBT(ele, eleM);//建立二叉搜索树 int T;cin >> T;while (T--) {string str;char buffer[30] = "";cin >> str; //输入合法化学方程式 str = getAim(str);int n = 0;//n化学分子个数string::iterator it = str.begin();while (isdigit(*it)) {n = n * 10 + *it - '0';str.erase(it);}//获得化学式的个数,同时修改strn = (n == 0 ? 1 : n);int sum = getWeight(str);printf("%04d\n", n * sum);}return 0;}

10406: A.挑战密室

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 41  Solved: 18
[Submit][Status][Web Board]

Description

R组织的特工Dr. Kong 为了寻找丢失的超体元素,不幸陷入WTO密室。Dr. Kong必须尽快找到解锁密码逃离,否则几分钟之后,WTO密室即将爆炸。

 

Dr. Kong发现密室的墙上写了许多化学方程式中。化学方程式,也称为化学反应方程式,是化学式表示物质化学反应的式子。化学方程式反映的是客观事实。因此书写化学方程式要遵守两个原则:一是必须以客观事实为基础;二是要遵守质量守恒定律。

化学方程式不仅表明了反应物、生成物和反应条件。同时,化学计量数代表了各反应物、生成物物质的量关系,通过相对分子质量或相对原子质量还可以表示各物质之间的质量关系,即各物质之间的质量比。对于气体反应物、生成物,还可以直接通过化学计量数得出体积比。例如:2NaOH+H2SO4=Na2SO4+2H2O

 

经过多次试探、推理,Dr. Kong发现密码是4位数字,就隐藏在化学方程式等号后的第一个分子中,其分子量就可能是密码(若分子量不足4位,前面加0)。

好在Dr. Kong还记得墙上各化学方程式用到的化学元素的原子量如下:

 

N

C

O

Cl

S

H

Al

Ca

Zn

Na

14

12

16

35

32

2

27

40

65

23

 

你能帮Dr. Kong尽快找到密码吗?

Input

第一行: K     表示有K化学方程式;

接下来有K行,每行为一个化学方程式

2≤K≤8  ,化学方程式的长度不超过50, 所有原子,分子的数量不超过9.小括号最多一层.

Output

对于每个化学方程式输出一行:即密码。

Sample Input

32
C+O2=2CO
2NaOH+H2SO4=Na2SO4+2H2O
Ca2CO3+H2O=Ca2(OH)2+CO2

Sample Output

0056
0142
0116


Decimal integer conversion

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述
XiaoMing likes mathematics, and he is just learning how to convert numbers between different bases , but he keeps making errors since he is only 6 years old. Whenever XiaoMing converts a number to a new base and writes down the result, he always writes one of the digits wrong. For example , if he converts the number 14 into binary (i.e., base 2), the correct result should be "1110", but he might instead write down "0110" or "1111". XiaoMing never accidentally adds or deletes digits, so he might write down a number with a leading digit of " 0" if this is the digit she gets wrong. Given XiaoMing 's output when converting a number N into base 2 and base 3, please determine the correct original value of N (in base 10). (N<=10^10) You can assume N is at most 1 billion, and that there is a unique solution for N. 
输入
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8)
Each test case specifies:
* Line 1: The base-2 representation of N , with one digit written incorrectly.
* Line 2: The base-3 representation of N , with one digit written incorrectly.
输出
For each test case generate a single line containing a single integer , the correct value of N
样例输入
11010212
样例输出
14
来源
河南省第九届省赛
暴力搜索所有可能,数据量不大 题目链接:Clickhere
#include <cstdio>#include <iostream>#include <vector>#include <cmath> using namespace std;int main(){int T;cin >> T;while(T--) {string strB, strC;cin >> strB >> strC;vector<int> v2, v3;for (int i = 0; i < strB.length(); ++i) {int num = 0;for (int j = strB.length() - 1; j >= 0; --j) {if (i == j)num = num + !(strB[j] - '0') * pow(2, strB.length() - 1 - j);else num = num + (strB[j] - '0') * pow(2, strB.length() - 1 - j);}v2.push_back(num);} for (int i = 0; i < strC.length(); ++i) {int num[2] = {0};for (int j = strC.length() - 1; j >= 0; --j) {if(i == j) {int cur = 0;for (char ch = '0'; ch <= '2'; ++ch) {if (ch != strC[j])num[cur++] += (ch - '0') * pow(3, strC.length() - 1 - j);}}else {num[0] += (strC[j] - '0') * pow(3, strC.length() - 1 - j);num[1] += (strC[j] - '0') * pow(3, strC.length() - 1 - j);}}v3.push_back(num[0]);v3.push_back(num[1]);}for (int i = 0; i < v2.size(); ++i)for (int j = 0; j < v3.size(); ++j)if (v2[i] == v3[j]) {printf("%d\n", v2[i]);break;}v2.clear();v3.clear();}return 0;} 






0 0
原创粉丝点击