【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://cnblogs.cn/nowonder
*/ #include <stdio.h>
#include <string.h>
#include <ctype.h> const int num[] = {0,1,2,3,4,5,6,7,8,9}; //使用数组将字符数字转为数字,ch - '0'即为该数字的下标
char s[80]; //储存字符串,n<=80 double get_mass(char ch)
{
//获得对应字符的分子量 if (ch=='C') return 12.01;
if (ch=='H') return 1.008;
if (ch=='O') return 16.00;
if (ch=='N') return 14.01;
} int main()
{
int T;
double sum;
scanf("%d",&T);
while (T--) //计数器
{
sum = 0;
scanf("%s",s);
int len = strlen(s);
if (len < 2) //对于单个字符,直接输出,否则造成溢出
{
printf("%.3lf\n",get_mass(s[0]));
continue;
} for (int i=0;i<len-2;i++) //防止溢出,从0到len-3
{
if (!isalpha(s[i])) continue; //数字跳过 if (!isalpha(s[i+1])) //一个元素带一个数字(个位数)
{
if (!isalpha(s[i+2])) //一个元素带两个数字(十位数)
{
sum += get_mass(s[i]) * (num[s[i+1] - '0'] * 10 + num[s[i+2] - '0']);
}
else
{
sum += get_mass(s[i]) * num[s[i+1] - '0'];
}
}
else //单独元素,直接算
{
sum += get_mass(s[i]);
}
} //字符串s中剩下两个字符
//如果全是数字直接跳 if (isalpha(s[len-2]))
{
if (!isalpha(s[len-1])) //元素带个位数
{
sum += get_mass(s[len-2]) * num[s[len-1] - '0'];
}
else //单独元素
{
sum += get_mass(s[len-2]);
}
} if (isalpha(s[len-1])) sum += get_mass(s[len-1]); //最后一个字符
printf("%.3lf\n",sum); //输出
}
return 0;
}
【OI】计算分子量 Molar mass UVa 1586 题解的更多相关文章
- Molar mass UVA - 1586
An organic compound is any member of a large class of chemical compounds whose molecules contain c ...
- 分子量(Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[20]; scanf ...
- UVa 1586 Molar mass --- 水题
UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)
习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...
- 分子量 (Molar Mass,ACM/ICPC Seoul 2007,UVa 1586)
解题思路: 1.将分子量用double 数组记录下来 2.将字符串存储在字符数组中,从头向后扫描,一直记住“字母”,对下一个字符进行判断,是否是数字,如果是数字:用一个整数记录,本代码中用的sum,同 ...
- UVa 1586 Molar mass
题意:给出物质的分子式,计算它的相对原子质量 因为原子的个数是在2到99之间的,所以找出一个是字母之后,再判断一下它的后两位就可以找出这种原子的个数了 #include<iostream> ...
- 【习题 3-2 UVA - 1586】Molar mass
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 主要是找那个数字. [代码] #include <bits/stdc++.h> using namespace ...
- UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言
关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...
- uva 1586 Molar mass(Uva-1586)
这题做的相当的复杂...之前做的现在应该能简单一点了写的. 我的代码: #include <bits/stdc++.h> using namespace std; main() { int ...
随机推荐
- 关于java新特性lambda表达式的理解即使用
Lambda 表达式的使用 1.举例: (o1,o2) -> Integer.compare(o1,o2); 2.格式: -> : lambda操作符 或 箭头操作符 ->左边 : ...
- 阿里面试Redis常考问题
一提到Redis缓存,我们不得不了解的三个问题就是:缓存雪崩.缓存击穿和缓存穿透.这三个问题一旦发生,会导致大量的请求直接请求到数据库层.如果并发压力大,就会导致数据库崩溃.那p0级的故障是没跑了. ...
- ES6继承和ES5继承是完全一样的么?
继承方式 ES5 prototype 继承 通过原型链(构造函数 + [[prototype]])指向实现继承. (备注:后续__proto__我都会写成[[prototype]]这种形式) 子类的 ...
- 剖析虚幻渲染体系(10)- RHI
目录 10.1 本篇概述 10.2 RHI基础 10.2.1 FRenderResource 10.2.2 FRHIResource 10.2.3 FRHICommand 10.2.4 FRHICom ...
- mac 软件意外退出
大概率的原因是软件签名问题. 先安装 xcode xcode-select --install 然后签名 sudo codesign --force --deep --sign - 文件位置(直接将应 ...
- 000 PCI Express协议入门指南目录
一.001 PCI Express体系结构(一)
- efcore分表下"完美"实现
ShardingCore 如何呈现"完美"分表 这篇文章是我针对efcore的分表的简单介绍,如果您有以下需求那么可以自己选择是否使用本框架,本框架将一直持续更新下去,并且免费开源 ...
- Docker源码安装附内网镜像安装演示
Docker源码安装附内网镜像安装演示 系统版本要求 当前系统版本:CentOS Linux release 7.9.2009 (Core) 内核版本:3.10.0-1160.el7.x86_64 注 ...
- 【AI】TorchVision_DataLoad
From: https://liudongdong1.github.io/ All datasets are subclasses of torch.utils.data.Dataset i.e, t ...
- 项目报错:Invalid bound statement (not found):
出现这种错误有好多种情况,常见的错误有以下这些: 1.检查xml文件所在package名称是否和Mapper interface所在的包名 <mapper namespace="com ...