这题做的相当的复杂。。。之前做的现在应该能简单一点了写的。

我的代码:

#include <bits/stdc++.h>
using namespace std;
main()
{
int a;
cin>>a;
while(a--)
{
string b="";
cin>>b;
stringstream stream;
int sumc,sumh,sumo,sumn;
sumc=sumh=sumo=sumn=;
int c,h,o,n,f;
c=h=o=n=;f=;
string str="";
for(int i=;i<b.size()+;i++)
{
if(isalpha(b[i])&&isalpha(b[i+])||isalpha(b[i])&&!b[i+])
{
if(b[i]=='C')
sumc+=;
if(b[i]=='H')
sumh+=;
if(b[i]=='O')
sumo+=;
if(b[i]=='N')
sumn+=;
}
else if(isdigit(b[i])&&f)
{
if(b[i-]=='C')
{
str+=b[i];c=;f=;
}
if(b[i-]=='H')
{
str+=b[i];h=;f=;
}
if(b[i-]=='O')
{
str+=b[i];o=;f=;
}
if(b[i-]=='N')
{
str+=b[i];n=;f=;
}
}
else if(c&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;c=;
stream<<str;
stream>>num;
sumc+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
else if(h&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;h=;
stream<<str;
stream>>num;
sumh+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
else if(o&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;o=;
stream<<str;
stream>>num;
sumo+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
else if(n&&!f)
{
if(isalpha(b[i])||!b[i])
{
int num=;
f=;n=;
stream<<str;
stream>>num;
sumn+=num;
stream.clear();
str="";
}
else
str+=b[i];
}
}
double sum=;
sum=sumc*12.01+sumh*1.008+sumo*16.00+sumn*14.01;
printf("%.3lf\n",sum);
}
}

uva 1586 Molar mass(Uva-1586)的更多相关文章

  1. UVa 1586 Molar mass --- 水题

    UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...

  2. Molar mass UVA - 1586

    ​ An organic compound is any member of a large class of chemical compounds whose molecules contain c ...

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

    题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...

  4. UVa 1586 Molar mass

    题意:给出物质的分子式,计算它的相对原子质量 因为原子的个数是在2到99之间的,所以找出一个是字母之后,再判断一下它的后两位就可以找出这种原子的个数了 #include<iostream> ...

  5. UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言

    关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...

  6. 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)

    习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...

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

    解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同 ...

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

    #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf ...

  9. 【习题 3-2 UVA - 1586】Molar mass

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 主要是找那个数字. [代码] #include <bits/stdc++.h> using namespace ...

随机推荐

  1. LeetCode 389. Find the Difference (找到不同)

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  2. CSS垂直居中完美有用实例

    <!DOCTYPE HTML>           <html>            <head>             <meta meta chars ...

  3. MapReduce04

    ===================== MapReduce内部机制:本地性 ===================== 什么是数据本地性(data locality)--------------- ...

  4. How to create a random UUID in each Request's Headers

    ${=java.util.UUID.randomUUID()}  is OK

  5. Linux ALSA声卡驱动之七:ASoC架构中的Codec

    1.  Codec简介(ad/da) 在移动设备中,Codec的作用可以归结为4种,分别是: 对PCM等信号进行D/A转换,把数字的音频信号转换为模拟信号 对Mic.Linein或者其他输入源的模拟信 ...

  6. Solution:Cannot pull with rebase: You have unstaged changes in Github

    You can do this to work around using following steps 1. stash your changes with: git stash 2. pull f ...

  7. YTU 2715: 函数---判断某年某月某日是这一年中的第几天

    2715: 函数---判断某年某月某日是这一年中的第几天 时间限制: 1 Sec  内存限制: 128 MB 提交: 380  解决: 155 题目描述 在主程序(main)中输入某年某月某日,例如2 ...

  8. JSP-Runoob:JSP 国际化

    ylbtech-JSP-Runoob:JSP 国际化 1.返回顶部 1. JSP 国际化 在开始前,需要解释几个重要的概念: 国际化(i18n):表明一个页面根据访问者的语言或国家来呈现不同的翻译版本 ...

  9. handbook/CentOS/使用免费SSL证书让网站支持HTTPS访问.md

  10. Gym - 100920E 2010-2011 OpenCup IX Onsite, II Yandex Summer School E.Paint 状压DP

    题面 题意:给你n(20)个点,m(40条边),让你给每条边染一种颜色,白色0元,红色2元,蓝色1元,现在要保证每一条白边相邻的有一条红边,问至少花多少 题解:刚开始想的时候,好像觉得只用染红色和白色 ...