691C - Exponential notation - CodeForces Solution


implementation strings *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define speedup ios_base::sync_with_stdio(false);cin.tie(NULL);
#define getUnique(v) {sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());}
const int mod=1e9+7;
double binexp(double n,ll p){if(p==0)return 1;double res=binexp(n,p/2);if(p&1) return res*res*n;return res*res;}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
 
void solve(){
    string s;
    cin>>s;
    int i=0;
    // bool vis=0;
    int cnt=0;
    
    if((int)s.size()==1 && s[0]=='0'){
        cout<<0;
        return;
    }
    vector<int>a,b;
    if(s[0]=='0'){
        while(s[i]=='0'){
            i++;
        }
    }
    if(find(s.begin(),s.end(),'.')==s.end()){
        int y=s.size()-1;
        int cnt=0;
        while(s[y]=='0')y--,cnt++;
        if((int)s.size()-i-cnt==1){
            cout<<s[y];
            if(cnt!=0)
            cout<<"E"<<cnt;
            return;
        }
        else{
            int j=i;
            cout<<s[i]<<".";
            i++;
            for(;i<=y;i++)cout<<s[i];
            if((int)s.size()-1!=0)
            cout<<"E"<<(int)s.size()-1-j;
            return;
        }
        
    }
    if(s[i]!='.')
    while(s[i]!='.'){a.pb(s[i]-'0');i++;cnt++;}
    i++;
    int j=(int)s.size()-1;
        if(s[j]=='0')
        while(s[j]=='0')
            j--;
        
    for(;i<=j;i++)b.pb(s[i]-'0');
    // for(auto x:a)cout<<x;
    //     cout<<"\n";
    // for(auto x:b)cout<<x;
    if((int)a.size()==0){
        if(b.size()==0){
            cout<<0;return;
        }
        int y=0;
        while(b[y]==0)y++;
        if((int)b.size()-y==1){
            cout<<b[y]<<"E";
            cout<<-(y+1);return;
        }else{
            cout<<b[y];
            cout<<".";
            for(int i=y+1;i<b.size();i++)cout<<b[i];
            cout<<"E"<<-(y+1);
        }
        
    }else{
        if((int)b.size()==0){
            int cnt=0;
            int y=a.size()-1;
            while(a[y]==0 && y>=0)cnt++,y--;
            //cout<<b.size();
            
            if((int)a.size()-cnt==1){
                cout<<a[0];
                if(cnt!=0){
                cout<<"E";
                cout<<(cnt);
                }
            }else{
                cout<<a[0];
                cout<<".";
                for(int i=1;i<=y;i++){
                    cout<<a[i];
                }
                if((int)a.size()-1!=0){
                cout<<"E";cout<<(int)a.size()-1;
                }
            }
        }else{
            cout<<a[0];
            cout<<".";
            for(int i=1;i<a.size();i++)cout<<a[i];
            for(auto x:b)cout<<x;
            if((int)a.size()-1!=0){
            cout<<"E";
            cout<<(int)a.size()-1;
            }
        }
    }
    
}
int main(){
    speedup;
    int t;t=1;
    // cin>>t;
    while(t--)solve();
}


Comments

Submit
0 Comments
More Questions

1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits