chaper3_exerise_Uva1568_Molar_Mass_分子量

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<string>
 4 #include<cctype>
 5 using namespace std;
 6 const int maxn = 1010;
 7
 8 void get_num(int &num,string c,int j)
 9 {
10     while (isdigit(c[j]) && c[j])
11     {
12         num = num*10 + (c[j]-‘0‘);
13         j++;
14     }
15 }
16
17 int main(void)
18 {
19     string c;
20     int T;
21     cin >> T;
22     for (int i = 0; i < T; i++)
23      {
24          double count = 0;
25          cin >> c;
26         for (int j = 0; j < c.size(); j++)
27         {
28             if (isalpha(c[j]))
29             {
30                 if (isdigit(c[j+1]))
31                 {
32                     int num = 0;
33                     get_num(num,c,j+1);
34                     switch(c[j])
35                     {
36                         case ‘C‘ : count += 12.01*num; break;
37                         case ‘H‘ : count += 1.008*num; break;
38                         case ‘O‘ : count += 16.00*num; break;
39                         case ‘N‘ : count += 14.01*num; break;
40                     }
41                 }
42                 else {
43                         switch(c[j])
44                         {
45                             case ‘C‘ : count += 12.01; break;
46                             case ‘H‘ : count += 1.008; break;
47                             case ‘O‘ : count += 16.00; break;
48                             case ‘N‘ : count += 14.01; break;
49                         }
50                 }
51             }
52         }
53         cout << fixed << setprecision(3) << count << endl;
54     }
55     return 0;
56 }
时间: 2024-08-01 22:47:58

chaper3_exerise_Uva1568_Molar_Mass_分子量的相关文章

课后题--------求分子量-----Molar mass------

简单的化学式  求分子量问题 下面附上  代码和解析. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 #include<math.h> 5 using namespace std ; 6 int main() 7 { 8 double l,m,n,t,sum,w; 9 int i,q; 10 char a[200]; 11 scanf("%lf",&

紫书 习题3-2 分子量(字符串,常量数组)

#include<stdio.h> #include<string.h> int main() { int t,i,num; char a[]={'C','H','O','N'}; double n[]={12.01,1.008,16.00,14.01};//此所谓之常量数组的妙用 char s[105]; double sum; scanf("%d",&t); while(t--) { sum=0;//每次多组数据输入时sum复位 scanf(&quo

分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同时下标++. 进行判断,查看是否对数字进行了记录,即查看sum是否进入了while循环并被赋值,如果没有被赋值,说明下一个字符不是数字,直接对W(总记录)值进行赋值,为当前字符的权值(分子量),即double数组的中的值.如果被赋值,说明字符后面是一个数字,sum中存放了该“数字”,也是对w赋值,不

【OI】计算分子量 Molar mass UVa 1586 题解

题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只有3种可能: 1.单个元素 2.一个元素和一位数字 3.一个元素和两位数字 没有了.因为题设交代了n<=99,表明个数只能为2位数.分别判断即可. /* Copyright 2019 AlexanderZ.Tang Molar_mass.cpp For UVa 1586 https://cnblog

分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf("%s", s); double sum = 0; for (int i = 0; i < strlen(s); i++) { if (s[i] == 'C') sum += (s[i + 1] - 48) * 12.01; if (s[i] == 'H') { if (s[i +

uva 1586 分子量 uva 1584 字典序最小

1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 #include<algorithm> 6 #include<map> 7 8 using namespace std; 9 10 int N; 11 12 map <char,double> ar; 13 14 int main(){ 15 ar['C'] =

分子量 UVA 1586

3900 - Molar mass Asia - Seoul - 2007/2008 An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass 

(UVA)1586 --Molar Mass(分子量)

题目链接:http://vjudge.net/problem/UVA-1586 思路:统计一个分子式中CHON出现的总次数,乘上相对原子量后求和.要注意的是CH4这样的C后面的1默认不出现,以及C4H10这样的后面的数字是两位的情况. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int t,len; 9

分子量(Molar Counting, ACM/ICPC Seoul 2007, UVa1586)

An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from