UVa1586 Molar mass

#include <stdio.h>

int GetQuantity(char* q, char** p)
{
    int quantity = 0;
    while (*q && ‘0‘ <= *q && *q <= ‘9‘)
    {
        quantity = quantity*10 + (*q-‘0‘);
        ++q;
    }
    if (quantity == 0)
        quantity = 1;
    *p = q;
    return quantity;
}

int main()
{
    int T, quantity;
    double mass;
    char str[81], *p;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%s", str);
        mass = 0.0;
        p = str;
        while (*p)
        {
            if (*p == ‘C‘)
            {
                quantity = GetQuantity(p+1, &p);
                mass += (12.01*quantity);
            }
            else if (*p == ‘H‘)
            {
                quantity = GetQuantity(p+1, &p);
                mass += (1.008*quantity);
            }
            else if (*p == ‘O‘)
            {
                quantity = GetQuantity(p+1, &p);
                mass += (16.00*quantity);
            }
            else if (*p == ‘N‘)
            {
                quantity = GetQuantity(p+1, &p);
                mass += (14.01*quantity);
            }
            else
            {
                ++p;
            }
        }
        
        printf("%.3f\n", mass);
    }

return 0;
}

时间: 2024-07-29 05:16:43

UVa1586 Molar mass的相关文章

3-2. Uva1586 Molar mass

#include<cstdio> #include<cstring> using namespace std; const double mol[4]={12.01,1.008,16.00,14.01}; void Do(){ char c[100]; double ans=0; memset(c,0,sizeof(c)); scanf("%s",&c); for(int i=0;c[i]!=0;i++){ double m=0; int n=0; in

UVA1586 UVALive3900 Molar mass

Regionals 2007 >> Asia - Seoul 问题链接:UVA1586 UVALive3900 Molar mass.基础练习题,用C++语言编写程序. 这个问题是根据分子式,求分子量. 原子量使用map表来存储,所以用C++来编程. 程序中,使用函数getchar()处理输入流,需要更高的编程技巧. AC的C++语言程序如下: /* UVA1586 UVALive3900 Molar mass */ #include <iostream> #include <

UVa 1586 Molar mass --- 水题

UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现一个从字符型的数到整型的数的转换函数,再将输入的串从头到尾扫描,遇到字母,则进一步扫描后面的数字的区间, 再调用函数进行转换,再乘以其的原子质量,最后累加到sum中即可. /* UVa 1586 Molar mass --- 水题 */ #include <cstdio> #include <

uva 1586 - Molar mass

在想更好的处理方法,现在却只能有这个糟烂的代码了--不好意思 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int maxn=200; char s[maxn]; double ans[maxn]; int get_num(int pos,int len) { int temp; for(int i=pos;i<len;i++) { if(s[i

UVa 1586 / UVALive 3900 Molar mass (字符串)

Molar mass Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description An organic compound is any member of a large class of chemical compounds whose molecules contain carbon. The molar mass of an organic compo

分子量(Molar Mass UVa1586)

题目来自张汝佳的<算法竞赛入门经典(第二版)> 题目描述: 我的代码: #include<iostream> #include<cstring> #include <iomanip> using namespace std; #define C 12.01 #define H 1.008 #define O 16.00 #define N 14.01 int main() { int i, k; char enter[1000]; cin >>

(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

【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 +