Problem
Test instructions
Give you a chemical formula that contains only Chon organic matter, such as C6H5OH, for relative molecular mass
Analysis
。。。
Code
#include <stdio.h> #include <cstring> #include <cctype># Define ll long longusing namespace Std;ll t;double w,m[5]= {12.01,1.008,16.00,14.01};char s[100];void Add (int f,int i) { if (IsDigit (s[i+1))) if (IsDigit (s[i+2])) w+=m[f]* ((s[i+1]-' 0 ') *10+s[i+2]-' 0 '); else w+=m[f]* (s[i+1]-' 0 '); else w+=m[f];} int main () {scanf ("%d", &t),///Note that there is a space behind%d, so it is not afraid to read back to the carriage return while (t--) {memset (s,0,sizeof (s)); scanf ("%s", s); w=0; for (int i=0; s[i], i++) {switch (S[i]) {case ' C ': Add (0,i); Break Case ' H ': Add (1,i); Break Case ' O ': Add (2,i); Break Case ' N ': Add (3,i); Break Default:break; }} printf ("%.3lf\n", W); } return 0;}
UVA 1586 Ancient Cipher