[] LuoguP1797 addition and subtraction _NOI Cruise Guide 2010 increase (05)

Subject description:

Cruise cows that human addition equation too far behind. For example, sometimes you want to calculate the Addition + 15 * 3, can only be written as + 15 + 15 + 15, what a waste of energy ah! So, Cruz decided to develop a new addition equation. Of course, the new formula also based on the original formula, the difference lies in the above equation can be written directly +++ 15, -15 * 3 course, for such a formula can be written as --- 15. After some time, Cruz has been that an infinite number of + - mouth to a number, so he will improve this formula a bit. For example + 15 * + 3 and can be written as (3) 15, of course, equivalent to -15 * 3 - (3) 15. It can be seen from the above, the case for smaller multipliers, such as 15 +++ such a statement is very easy, so the new equation is retained in this form ugly.

For special formula also make a point of explanation:

* 3 + 15 +++ 15 can be written or + (3) 15 is converted into Cruz type expression, but not written as + (2) 15 such forms.

For the 23 + 15 * Equation 3-2 can be expressed as the following forms:

23+++15-2

23+(3)15-2

+23+++15-2

+23+(3)15-2
+(1)23+(3)15-(1)2

The following forms will not:

    (1)23+++15-2
    +23++(2)15-(1)2
    23+++15-2+(0)100
    23-(-3)15-2

Input Format

Line, a cruise-type formula.

Output Format

Line, as result of the operation.

Sample input and output

Input # 1
+(1)23+(3)15-(1)2
Output # 1
66

Description / Tips

20% to the data input length does not exceed 10;

To 100% of the data length of the input does not exceed 200.

Thinking:    This is a simulation. Coupled with high precision. . Tune for two weeks

      This cow is sick of it, every day delirious

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char ch[210];
int a1[2000],a2[2000],sum[2000];//a1,a2暂时储存,sum储存乘出来的值 
int s[20000],ans[20000];
int f=1;

//读入 
inline void read1(int x){
    memset(a1,0,sizeof(a1));
    while(ch[x]<'0'||ch[x]>'9')x++;
    while(ch[x]>='0'&&ch[x]<='9')x++;
    x--;
    while(ch[x]>='0'&&ch[x]<='9')a1[++a1[0]]=ch[x]-'0',x--;
}
inline void read2(int x){
    memset(a2,0,sizeof(a2));
    while(ch[x]<'0'||ch[x]>'9')x++;
    while(ch[x]>='0'&&ch[x]<='9')x++;
    x--;
    while(ch[x]>='0'&&ch[x]<='9')a2[++a2[0]]=ch[x]-'0',x--;
}

//高精乘 
inline void mul(){
    sum[0]=a1[0]+a2[0]-1;
    for(int j=1;j<=a2[0];j++){
        for(int i=1;i<=a1[0];i++){
            sum[i+j-1]+=a1[i]*a2[j];
            sum[i+j]+=sum[i+j-1]/10;
            sum[i+j-1]%=10;
        }
    }
    if(sum[sum[0]+1]>=1) sum[0]++;
    while(sum[sum[0]]==0&&sum[0]>1)--sum[0];
}

//高精加 
inline void add(int a[],int b[]){
    memset(s,0,sizeof(s));
    int len=max(a[0],b[0]);
    int g=0;
    for(int i=1;i<=len;i++){
        s[i]+=a[i]+b[i]+g;
        g=s[i]/10;
        s[i]=s[i]%10;
    }
    if(g)s[++len]=g;
    s[0]=len;
    for(int i=0;i<=len;++i)ans[i]=s[i];
}

//高精减 
inline bool com(int a[],int b[]){
    if(a[0]>b[0])return 0;
    if(a[0]<b[0])return 1;
    for(int i=a[0];i>=1;--i){
        if(a[i]<b[i])return 1;
        if(a[i]>b[i])return 0;
    }
    return 0;
}
inline void jian(int a[],int b[]){
    if(com(a,b)){
        swap(a,b);
        f=-1;
    }
    else f=1;
    for(int i=1;i<=a[0];++i){
        if(i<=b[0])   //我不知道为啥可能在i<=a[0]&&i>=b[0]的时候b[i]有值
            a[i]-=b[i];
        if(a[i]<0){
            a[i]+=10;
            a[i+1]--;
        }
    }
    while(a[a[0]]==0&&a[0]>1)a[0]--;
    for(int i=0;i<=a[0];++i)
        ans[i]=a[i];
}

//把int型数拆到数组中 
inline void dig(int x){
    memset(a1,0,sizeof(a1));
    while(x){
        a1[++a1[0]]=x%10;
        x/=10;
    }
}
int main(){
    scanf("%s",ch+1);
    int len=strlen(ch+1);
    for(int i=1;i<=len;++i){
        if(ch[i]=='+'&&ch[i+1]=='('){
            read1(i+2);
            while(ch[i]<'0'||ch[i]>'9')i++;
            while(ch[i]>='0'&&ch[i]<='9')i++;
            i++;
            read2(i);
            mul();
            if(f==1)            //讨论符号,f为1,ans为正,f为-1,ans为负 
                add(ans,sum);
            else jian(sum,ans);
            memset(sum,0,sizeof(sum));
        }
        else if(ch[i]=='-'&&ch[i+1]=='('){
            read1(i+2);
            while(ch[i]<'0'||ch[i]>'9')i++;
            while(ch[i]>='0'&&ch[i]<='9')i++;
            i++;
            read2(i);
            mul();
            if(f==1)
                jian(ans,sum);
            else add(ans,sum);
            memset(sum,0,sizeof(sum));
        }
        else if(ch[i]=='+'&&ch[i+1]>='0'&&ch[i+1]<='9'){
            read1(i+1);
            if(f==1)
            add(ans,a1);
            else jian(a1,ans);
        }
        else if(ch[i]=='-'&&ch[i+1]>='0'&&ch[i+1]<='9'){
            read1(i+1);
            if(f==1)jian(ans,a1);
            else add(ans,a1);
        }
        else if(ch[i]=='+'&&ch[i+1]=='+'){
            int cnt=0;
            while(ch[i]=='+')cnt++,i++;
            dig(cnt);
            read2(i);
            mul();
            if(f==1)add(ans,sum);
            else jian(sum,ans);
            memset(sum,0,sizeof(sum));
        }
        else if(ch[i]=='-'&&ch[i+1]=='-'){
            int cnt=0;
            while(ch[i]=='-')cnt++,i++;
            dig(cnt);
            read2(i);
            mul();
            if(f==1)
                jian(ans,sum);
            else add(ans,sum);
            memset(sum,0,sizeof(sum));
        }
        else if(i==1&&ch[i]>='0'&&ch[i]<='9'){
            read1(i);
            for(int j=0;j<=a1[0];++j)
                ans[j]=a1[j];
        }
    }
    if(f==-1)printf("-");
    for(int i=ans[0];i>0;--i)
        printf("%d",ans[i]);
    return 0;
}

 

  

 

 

Guess you like

Origin www.cnblogs.com/yelir/p/11536019.html