Nyoj 1236 Challenge Chamber of Secrets (Henan Province, the first ACM Program Design Competition)

Source: Internet
Author: User
Tags printf relative time limit
Topic 1236 topic Information running results Discussion area Challenge Chamber of SecretsTime limit: Ms | Memory limit: 65535 KB Difficulty: 4 description

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 that the walls of the Chamber of Secrets were written in many chemical equations. A chemical equation, also known as a chemical reaction equation, is a chemical expression of a substance's chemical reaction. The chemical equation reflects an objective fact. Therefore, the formulation of chemical equations to abide by two principles: first, must be based on objective facts, the second is to abide by the law of mass conservation.

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+H2SO4=NA2SO4+2H2O

After many tests and inferences, Dr Kong found that the password is a 4-digit number, which is hidden in the first molecule after the chemical equation equals sign, and its molecular weight may be the password (if the molecular weight is less than 4 bits, preceded by 0).

Fortunately, Dr. Kong also remembers the atomic weight of the chemical elements used on the walls of the wall 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 to find the password soon. Enter the first line: K, indicating that there is a k chemical equation;
Then there's K-line, one chemical equation for each action.
Output one line for each chemical equation: that is, the password. Sample input

32C+O2=2CO2NAOH+H2SO4=NA2SO4+2H2OCA2CO3+H2O=CA2 (OH) 2+co2
Sample output
005601420116
It is suggested that the chemical equation is not more than 50 in length, and the number of atoms and molecules is not more than 9 2≤k≤8. The parentheses are at most one level. Source eighth session of Henan Province Program design Contest uploader

Hnu_acm


Do not use the stack what is directly.

#include <stdio.h> #include <string.h> char che[10][3]={"N", "C", "O", "Cl", "S", "H", "Al", "Ca", "Zn", "Na"};
int val[10]={14,12,16,35,32,2,27,40,65,23}; 
int Tonum (char str[]) {for (int i=0;i<10;i++) if (strcmp (Str,che[i]) ==0) return val[i];
	} int main () {int ncase;
	Char str[500];
	scanf ("%d", &ncase);
		while (ncase--) {memset (str,0,sizeof (str));
		scanf ("%s", str);
		BOOL Flag=false;
		int k=0;
		Char word[55];
		memset (word,0,sizeof (word));
			For (int. i=0;i<strlen (str); i++) {if (flag&&str[i]== ' + ') break;
				if (str[i]== ' = ') {flag=true;
			Continue
			} if (flag) {word[k++]=str[i];
		}} char ch[3];
		int sum=0,head=1,star=0,n;
		if (word[0]>= ' 0 ' &&word[0]<= ' 9 ') head=word[0]-' 0 ', star=1;
			for (int i=star;i<k;i++) {memset (ch,0,sizeof (ch));
			N=1;
				if (word[i]== ' (') {i++;
				int temp=0;
					while (word[i]!= ') ' &&i<k) {memset (ch,0,sizeof (ch)); if (word[i]>= ' A ' &&Amp;word[i]<= ' z ') && (word[i+1]>= ' a ' &&word[i+1]<= ' Z ')) {if (word[i+2]>= ' 0 ' &&w
						ord[i+2]<= ' 9 ') n=word[i+2]-' 0 ';
						ch[0]=word[i];ch[1]=word[i+1];ch[2]= ' + ';
						i++;
					Temp+=n*tonum (CH); } else if ((word[i]>= ' A ' &&word[i]<= ' Z ') &&! ( Word[i+1]>= ' a ' &&word[i+1]<= ' Z ') {if (word[i+1]>= ' 0 ' &&word[i+1]<= ' 9 ') n=word[
						i+1]-' 0 ';
						ch[0]=word[i];ch[1]= ' + ';
					Temp+=n*tonum (CH);
				} i++;
				} if (word[i+1]>= ' 0 ' &&word[i+1]<= ' 9 ') n=word[i+1]-' 0 ';
			Sum+=n*temp;
				} else if ((word[i]>= ' a ' &&word[i]<= ' Z ') && (word[i+1]>= ' a ' &&word[i+1]<= ' Z ')) {
				if (word[i+2]>= ' 0 ' &&word[i+2]<= ' 9 ') n=word[i+2]-' 0 ';
				ch[0]=word[i];ch[1]=word[i+1];ch[2]= ' + ';
				i++;
			Sum+=n*tonum (CH); } else if ((word[i]>= ' A ' &&word[i]<= ' Z ') &&! ( Word[i+1]>= ' A ' &&word[i+1]<= ' Z ')) {if (word[i+1]>= ' 0 ' &&word[i+1]<= ' 9 ') n=word[i+1]-' 0 ';
				ch[0]=word[i];ch[1]= ' + ';
			Sum+=n*tonum (CH);
		}} Sum=sum*head;
		if (sum<10) printf ("000%d\n", sum);
		else if (sum<100) printf ("00%d\n", sum);
		else if (sum<1000) printf ("0%d\n", sum);
	else printf ("%d\n", sum);
} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.