1586 - Molar mass

All about problems in Volume 15. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
bijoy 71
New poster
Posts: 1
Joined: Mon Jul 06, 2015 1:09 am
Location: Sylhet,Bangladesh

1586 - Molar mass

Post by bijoy 71 » Mon Sep 07, 2015 8:28 pm

why i am getting Wrong Answer ?
my Code:

Code: Select all

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <cctype>
#include <vector>
#include <cstdlib>
#define pb push_back
#define pf printf
#define sf scanf
#define N 81
using namespace std;

struct my
{
    char c;
    double fl;
};

int main()
{
    int t,len=0,d,b,slen=0; double r;
    vector <char> vc;
    vector <int> v;
    vector <float> vf;
    char ch[N]; my ar[N];
    //float ar[N];
    sf("%d",&t);

    getchar();

    while (t--)
    {
        gets(ch); b=0;

        ar[slen].c=ch[0];

        for (int i=0;i<=strlen(ch);i++)
        {
           r = 0.000;

           if (ch[i]=='C' or ch[i]=='H' or ch[i]=='O' or  ch[i]=='N' or ch[i]=='\0')
           {
               d=i; //ar[slen].c = ch[i];

               if (d==0) continue;

               else
               {
                   for (int j=b+1;j<d;j++)
                   {
                       r = (ch[j]-'0')+(r*10);
                   }

                   //v.pb(r);
                    if (r==0)
                    {
                        r+=1.000;
                    }
                    ar[slen].fl = (double) r;  slen++;
                   ar[slen].c = ch[i];
                   //slen++;
                   b = d;
               }

               //ar[slen].c = ch[d];
               //ar[slen].fl = (float) r; slen++;
               //slen++;

               //b = d;
           }
        }

        /*for (int i=0;i<slen;i++)
        {
            pf ("%c%f; ",ar[i].c,ar[i].fl);
        }*/

        double s = 0.00;

        for (int i=0;i<slen;i++)
        {
            if (ar[i].c == 'C')
            {
                s+=(ar[i].fl * 12.01);
            }

            else if (ar[i].c == 'H')
            {
                s+=(ar[i].fl * 1.008);
            }

            else if (ar[i].c == 'O')
            {
                s+=(ar[i].fl * 16.00);
            }

            else if (ar[i].c == 'N')
            {
                s+=(ar[i].fl * 14.01);
            }

            else
            {
                continue;
            }
        }

        pf ("%0.3lf\n",1.00*s);

        slen=0;
    }

    return 0;
}
Last edited by brianfry713 on Tue Sep 08, 2015 1:06 am, edited 3 times in total.

Gator
New poster
Posts: 3
Joined: Wed May 29, 2013 8:28 am

Re: 1586 - Molar mass

Post by Gator » Thu Dec 17, 2015 4:05 am

Java users should look out for extraneous trailing whitespace. Without a call to trim() on reading a line of input using a BufferedReader --> WA. With trim() --> AC.

Code: Select all

String line = in.readLine().trim()

Eucliwood
New poster
Posts: 3
Joined: Mon May 30, 2016 1:07 pm

Re: 1586 - Molar mass

Post by Eucliwood » Wed Jun 22, 2016 9:38 am

Why am I getting WA? I've compared my outputs to uDebug outputs, and it produces the correct one

Code: Select all

#include<iostream>
#include<string>
#include<sstream>
#include<cstdio>
using namespace std;

long change (string text) { //converts string to int
    long result;
    stringstream convert(text);
    if( !(convert >> result)) {
        result=0;
    }
    return result;
}

int main() {
    int kasus, size, value, value_2;
    double sum, carry;
    string input, temp, check, c;
    bool correct;
    cin>>kasus;
    for( int n = 0 ; n < kasus ; n++) {
        if( n == 0) {
            cin.ignore();
            cin.clear();
        }
        getline(cin, input);
        size = input.size();
        sum = 0;
        value = 0;
        for( int i = 0 ; i < size ; i++) {
            temp = input.at(i);
            correct = false;
            if( temp == "C" ) {
                sum = sum + 12.01;
                carry = 12.01;
            }
            else if( temp == "H" ) {
                sum = sum + 1.008;
                carry = 1.008;
            }
            else if( temp == "N" ) {
                sum = sum + 14.01;
                carry = 14.01;
            }
            else if( temp == "O" ) {
                sum = sum + 16.00;
                carry = 16.00;
            }
            else {
                check = input.at(i);
                if( i < size - 1) {
                    c = input.at(i+1);
                    correct = true;
                }
                if( (c!="C") && (c!="H") && (c!="O") && (c!="N") && correct) {
                    value_2 = change(c) - 1;
                    value = (change(check)*10) + value_2;
                    i++;
                }
                else value = change(check) - 1;
                sum = sum + (carry * value);
            }
        }
        printf("%.3lf\n", sum);
    }
}


Post Reply

Return to “Volume 15 (1500-1599)”