UVA-1586 Molar Mass

原题网址

分析:简单题,对字符串判断与操作。

Source:

#include
#include
int main()
{
    double saw[4]={12.01,1.008,16.00,14.01},sum;
    int num[4]={0,0,0,0};
    int i,k,l,t,c;
    char s[80];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        l=strlen(s);
        sum=0;
        num[0]=num[1]=num[2]=num[3]=0;
        for(i=0;iif(s[i]=='C'||s[i]=='H'||s[i]=='O'||s[i]=='N')
            {
                if(s[i]=='C') k=0;
                else if(s[i]=='H') k=1;
                else if(s[i]=='O') k=2;
                else k=3;
                if(s[i+1]=='C'||s[i+1]=='H'||s[i+1]=='O'||s[i+1]=='N'||s[i+1]=='\0')
                    num[k]++;
                i++;
            }
            else
            {
                if(s[i+1]>='0'&&s[i+1]<='9')
                {
                    num[k]+=10*(s[i]-'0')+s[i+1]-'0';
                    i+=2;
                }
                else
                {
                    num[k]+=s[i]-'0';
                    i++;
                }
            }
        }
        for(i=0;i<4;i++)
            if(num[i])
                sum+=saw[i]*num[i];
        printf("%.3lf\n",sum);
    }
    return 0;
}

你可能感兴趣的:(UVA-1586 Molar Mass)