Ultraviolet A 10700-camel trading

Source: Internet
Author: User

Wa once, because the use of long is not considered completely. This question is similar to the brackets in the past. Maximum calculates the addition, saves the sum, and multiply the result. The minimum calculates the multiplication and adds the result.

#include<cstdio>#include<cstring>#include<iostream>#include<cctype>using namespace std;int main(){    //freopen("in.txt","r",stdin);    char str[350];    long long  int add[20],fac[20];    int numadd,numfac,n,flag;    scanf("%d",&n);    getchar();    while(n--)    {        gets(str);        flag=0;        numadd=0;        numfac=0;        long long  int num=0,sum=0,cc=1;        int i;        for( i=0; str[i]!='\0'; i++)        {            if(isdigit(str[i]))                num=num*10+str[i]-'0';            else if(str[i]=='+')            {                flag=1;                sum+=num;                cc*=num;                add[numadd++]=cc;                cc=1;                num=0;            }            else if(str[i]=='*')            {                flag=1;                sum+=num;                fac[numfac++]=sum;                sum=0;                cc*=num;                num=0;            }        }        if(flag==0)        {            cout<<"The maximum and minimum are "<<num<<" and "<<num<<".\n";            continue;        }        for(i--; isdigit(str[i])&&i>=0; i--);        if(str[i]=='+')        {            sum+=num;            fac[numfac++]=sum;            cc*=num;            add[numadd++]=cc;            cc=1;            num=0;        }        else if(str[i]=='*')        {            sum+=num;            fac[numfac++]=sum;            sum=0;            cc*=num;            add[numadd++]=cc;            num=0;        }        long long FAC=1;        long long  int ADD=0;        for(i=0; i<numadd; i++)            ADD+=add[i];        for( i=0; i<numfac; i++)            FAC*=fac[i];        cout<<"The maximum and minimum are "<<FAC<<" and "<<ADD<<".\n";    }    return 0;}

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.