10406:a. Challenge Chamber of Secrets time limit:1 Sec Memory limit:128 MB submit:29 solved:10 [Submit][status][web Board] Description
R the agents of the organization Dr Kong in order to find the missing BODY element, unfortunately caught WTO The chamber. Dr Kong must quickly find the unlocking code to flee, or a few minutes later, theWTO Chamber of Secrets is about to explode.
dr. Kong I found a lot of in the chemical equation. The chemical equation, also known as the chemical reaction equation, is the use 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+h2so 4=na2so4+2h 2O
after many temptations and inferences, Dr Kong The discovery password is 4 digits, it is hidden in Chemical equation The molecular weight of the first molecule after the equals sign may be a cipher (if the molecular weight is insufficient 4 bit, front plus 0 ).
fortunately Dr Kong remember 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 Kong find the code ASAP?
Input
First line: K indicates a K chemical equation;
Next there K Line, each action a chemical equation
2 ≤K≤8, the chemical equation length does not exceed , all atoms , the number of molecules not more than 9. Parentheses up to one level .
Output
for each Chemical Equations output One line: that is password.
Sample Input
32C+O2=2CO2NAOH+H2SO4=NA2SO4+2H2OCA2CO3+H2O=CA2 (OH) 2+co2
Sample Output
005601420116
The problem: Pay attention to brackets, the front coefficient, very easy to make mistakes, but again a very happy;
Code:
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include < algorithm>using namespace std; #define MEM (x, y) memset (x,y,sizeof (×)) #define SI (x) scanf ("%d", &x) #define SL (x) scanf ("%lld", &x) #define PI (x) printf ("%d", x) #define PL (x) printf ("%lld", x) #define P_ printf ("") const int INF=0X3F3 F3f3f;const double Pi=acos ( -1.0), Char s[55];int c[30];int main () {int k;si (K); c[' N '-' a ']=14;c[' C '-' a ']=12;c[' O '-' a ']= 16;c[' s '-' a ']=32;c[' H '-' a ']=2;while (k--) {scanf ("%s", S); int Flot=0,ans=0,temp,cur=0,k=1,kh=0,x=0;int len= Strlen (s); for (int i=0;i<len;i++) {if (s[i]== ' = ') {flot=1;continue;} if (!flot) continue;if (IsDigit (S[i])) {if (!cur) {k=s[i]-' 0 '; continue;} else if (kh==1) {x=temp* (s[i]-' 0 '); cur=0;continue;} ans+=temp* (s[i]-' 0 '); cur=0;} else if (Isalpha (S[i])) {if (kh==1&&cur) x+=temp;else if (cur) ans+=temp;if (i+1<len&&s[i]== ' C ' & &s[i+1]== ' l ') {temp=35;i++;} else if (i+1<len&&s[i]== ' A ' &&s[i+1]== ' l ') {temp=27;i++;} else if (i+1< len&&s[i]== ' C ' &&s[i+1]== ' a ') {temp=40;i++;} else if (i+1<len&&s[i]== ' Z ' &&s[i+1]== ' n ') {temp=65;i++;} else if (i+1<len&&s[i]== ' N ' &&s[i+1]== ' a ') {temp=23;i++;} else temp=c[s[i]-' A '];cur=1;} else if (s[i]== ' (') {kh=1;if (cur) ans+=temp;cur=0;} else if (s[i]== ') ') {if (cur) x+=temp;cur=0;if (isdigit (s[i+1])) ans+=x* (s[i+1]-' 0 '), I++;else ans+=x;cur=0;kh=0;} else break;} if (cur) ans+=temp;cur=0;printf ("%04d\n", Ans*k);} return 0;}
Eighth annual ACM Provincial a Challenge Chamber of Secrets (analog)