Molecular weight (Molar mass,acm/icpc Seoul 2007,uva 1586)

Source: Internet
Author: User

Problem Solving Ideas:

1. Record the molecular weight with a double array

2. Store the string in a character array, scan from the back, always remember "letter", the next character to determine whether it is a number, if it is a number: With an integer record, this code with the sum, subscript + +.

Judge to see if the number is recorded, that is, whether the sum has entered the while loop and is assigned, if not assigned, the next character is not a number, and the W (total record) value is directly assigned to the current character's weight (molecular weight), which is the value in the double array. If the value is assigned, the description character is followed by a number, the sum holds the number, and the W is assigned, but at this time the weight (molecular weight) of the current character that is assigned sum.

3. Finally the entire string loop is complete, the output total record W can be.

#include <stdio.h>int main (void) {    double darr[200];    int n,sum,p;    Double W;    Char c,carr[1000];    darr[' C ']=12.01;    darr[' H ']=1.008;    darr[' O ']=16.00;    darr[' N ']=14.01;    scanf ("%d", &n);    while (n--)    {        p=0;        w=0;        scanf ("%s", Carr);        while (carr[p]!= ')        {            sum=0;            C=CARR[P];            p++;            while (carr[p]>= ' 0 ' && carr[p]<= ' 9 ')            {                sum=sum*10+carr[p]-' 0 ';                p++;            }            if (sum)                w=w+sum* (Darr[c]);            else                w+= (darr[c]);        }        printf ("%.3f\n", W);    }    return 0;}

  

Molecular weight (Molar mass,acm/icpc Seoul 2007,uva 1586)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.