Title:Challenge Chamber of Secrets
time limit: + MS | Memory limit:65535 KB
Difficulty: 4
Describe
The R-organized agent Dr. Kong was unfortunately caught in the WTO chamber of Secrets in search of the missing super-body element. Dr Kong must quickly find the unlocking code to flee, or a few minutes later, the WTO chamber of Secrets is about to explode.
dr. kong found the walls of the Chamber of Secrets written on many chemical equations. The chemical equation, also known as the chemical reaction equation, is chemical denotes substance chemical reaction
the chemical equation not only shows the reactants, the produce and the reaction conditions. At the same time, the stoichiometric number represents the quantity relationship between the reactants and the generated matter, which can also be expressed by the relative molecular mass or the relative atomic mass, which is the mass ratio between the substances. For gaseous reactants and generators, volume ratios can also be obtained directly from stoichiometric numbers. Example:2naoh+h2 so4=na2so4+2h 2 O
after many tests and inferences, Dr Kong discovered that the password was 4 digits and was hidden in The chemical equation is the first molecule after the equals sign, and its molecular weight may be the cipher (if the molecular weight is less than 4 bits, the front plus 0).
fortunately Dr. Kong remembers the chemistry on the wall. equation The atomic weight of the chemical elements used in the formula is as follows:
N |
C |
O |
Cl |
S |
H |
Al |
Ca |
Zn |
Na |
14 |
12 |
16 |
35 |
32 |
2 |
27 |
40 |
65 |
23 |
Can you help Dr. Does Kong find the code ASAP?
Input
The first line: K, indicating that there is a k chemical equation;
Then there's K-line, one chemical equation for each action.
Output
For each chemical equation output one line: That is, the password.
Sample input
3
2c+o2=2co
2naoh+h2so4=na2so4+2h2o
CA2CO3+H2O=CA2 (OH) 2+co2
Sample output
0056
0142
0116
Tips
2≤k≤8, the chemical equation is not longer than 50, and the number of atoms and molecules does not exceed 9. The parentheses are at most one level.
Source
Eighth session of Henan Province Program design Competition
Main topic:give an equation, and ask for its relative atomic mass. Topic Ideas:
1, first find out the relative mass of each atom,2. Start traversing the equation before the equals sign. Called before the parentheses are met, and the inner values are returned when the parentheses are encountered3, each small molecule will have a digital 3H2O, 3 is 3 (H2O), 2 is two (h), and finally calculated.
4. Other direct simulations on the line
Program:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath > #include <algorithm> #include <cctype> #include <fstream> #include <limits> #include < vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <cassert>using namespace Std;char s[100];int cha (int i) {char c=s[i],lc=s[i-1],nc=s[i+1]; if (c<= ' z ' &&c>= ' a ')//not molecular return 0; if (c<= ' Z ' &&c>= ' a ')//convert to mass {if (c== ' N ' &&nc== ' a ') return 23; else if (c== ' Z ' &&nc== ' n ') return 65; else if (c== ' C ' &&nc== ' a ') return 40; else if (c== ' A ' &&nc== ' l ') return 27; else if (c== ' H ') return 2; else if (c== ' S ') return 32; else if (c== ' C ' &&nc== ' l ') return 35; else if (c== ' O ') return 16; Elseif (c== ' C ') return 12; else if (c== ' N ') return 14; } else return-1;} int main () {int ci,n,m,t,i,j,k,len; scanf ("%d", &ci); while (ci--) {int next=0,kuo=0,nei=0,da=0,bei=1; scanf ("%s", s); Len=strlen (s); for (i=0; s[i]!= ' = '; i++);//Seek the quality i++ of the latter; if (Cha (i) ==-1)//not for the molecule {sscanf (s+i, "%d", &bei);//Remove the number T=bei; while (t)//multiple numbers make up {t/=10; i++; }} for (; s[i]!= ' + ' &&i<len; i++) {if (Cha (i) ==0) continue; if (Kuo)//judgment Bracket {if (s[i]== ') ') {next+=nei; nei=0; kuo=0; } else if (Cha (i) ==-1) {sscanf (s+i, "%d", &t); Nei=nei*t; while (T/10) {t/=10; i++; }} else if (Cha (i) >0) {next+=nei; Nei=cha (i); }} else {if (s[i]== ' (') {da+=next; Kuo=1; next=0; } else if (Cha (i) ==-1) {sscanf (s+i, "%d", &t); Next=next*t; while (T/10) {t/=10; i++; }} else if (Cha (i) >0) {da+=next; Next=cha (i); }}//cout<<da<< ' \ t ' <<next<< ' \ t ' <<nei<< ' \ t ' <<s[i]<<e Ndl } printf ("%04d\n", (Da+next) *bei); } return 0;}
Nyoj 89 Challenge Chamber of Secrets