C++ 6ms


  • 0
    E
    class Solution {
    public:
            char *s1,*s2;
    
        string fractionAddition(string expression) {
            string s = expression;
            int a[12][3];
            memset(a,0,sizeof(a));
            int len = s.length();
            int i=0,num=0,k,j,l,flag=0,fz,fm,gbs,gys;
            while(i<len){
                if(i==0&&s[i]=='-'){
                    a[0][0]=1;
                    i++;
                }else
                if(i!=0&&s[i]=='-'){
                    a[num][0]=1;
                    i++;
                }else if(i!=0){
                    i++;
                }
               cout<<"2  --"<<i<<endl;
    
                // i++;
                k = s[i]-'0';
                if(valid(s[i+1])){
                    k = k*10+s[i+1]-'0';
                    i++;
                }
                i++;
                cout<<"1  --"<<i<<endl;
                // i++;
                while(!valid(s[i])){
                    i++;
                };
                cout<<"3  --"<<i<<endl;
    
                a[num][1]=k;
                l = s[i]-'0';
                if(i+1<len&&valid(s[i+1])){
                    l=l*10+s[i+1]-'0';
                    i++;
                }
                a[num][2]=l;
                
                i++;
                num++;
            }
            
            for(i=0;i<num;i++){
                int gy = measure(a[i][1],a[i][2]);
                a[i][1]/=gy;
                a[i][2]/=gy;
                // for(j=0;j<3;j++){
                    // cout<<a[i][j]<<" ";
                // }
                // cout<<endl;
            }
            gbs = 1;
            for(i=0;i<num;i++){
                gbs=LeastCommonMultiple(gbs,a[i][2]);
            }
            fz=0;
            for(i =0 ;i<num;i++){
                int zj = gbs/a[i][2];
                cout<<zj<<endl;
                if(a[i][0]==1){
                    fz=fz- a[i][1]*zj;
                }else{
                    fz=fz+ a[i][1]*zj;
                }
            }
            cout<<" 32 --"<<fz<<" "<<gbs<<endl;
            
            char re[50];
            // string re;
            int rlen=0;
            if(fz<0){
                flag=1;
                fz=-fz;
                rlen++;
                re[0]='-';
            }
            int zz = measure(fz,gbs);
            fz/=zz;
            gbs/=zz;
            char b[11],c[11];
            int l1=0,l2=0;
            if(fz==0){
                b[0]=0+'0';
                l1=1;
            }
            while(fz){
                b[l1++]=fz%10+'0';
                fz/=10;
            }
            
            while(gbs){
                c[l2++]=gbs%10+'0';
                gbs/=10;
            }
            for(i=l1-1;i>=0;i--){
                re[rlen++] = b[i];
            }
            re[rlen++]='/';
            for(i=l2-1;i>=0;i--){
                re[rlen++] = c[i];
            }
            string sss(&re[0],&re[rlen]);
            return sss;
            
            // int len1=trans(fz,s1);
            // int len2 = trans(gbs,s2);
            // cout<<"len"<<len1<<" "<<len2<<endl;
            
            // s[rlen++]='/';
            // return re;
        }
        
        bool valid(char ch){
            return ch>='0'&&ch<='9';
        }
        
        int LeastCommonMultiple(int a, int b)  
        {  
            int temp = a * b / measure(a, b);  
            return temp;  
        }
        int measure(int x, int y)  
        {     
            int z = y;  
            while(x%y!=0)  
            {  
                z = x%y;  
                x = y;  
                y = z;    
            }  
            return z;  
        } 
        
        int trans(int x,char* s){
            int len=0;
            while(x){
                s[len]=x%10;
                x/=10;
                len++;
            }
            return len;
        }
        
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.