implementation strings *1200

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <utility>
#include <vector>
#include <iostream>
#include <vector>
#include <numeric>
#define ll long long
#include <iomanip>
using namespace std;

void solve()
{
    string s;
    cin >> s;
    // x=0.0982;
    // y=x;
    // if(y<0)
    // {
    //     y=-y;
    // }
    // y = floor(y * 100) / 100;
    // y = round(y * 100) / 100;
    // cout<<s;
    // string s=to_string(y);
    // cout<<s<<endl;
    string s1="";
    string s2="";
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='.')
        {
            if(i+2<s.length())
            {
                s2+=s[i+1];
                s2+=s[i+2];
                break;
            }
            else if(i+1<s.length())
            {
                s2+=s[i+1];
                s2+='0';
                break;
            }
        }
        s1+=s[i];
    }
    // cout<<s1<<endl<<s2<<endl;
    if(s[0]=='-')
    {
        s1.erase(0, 1);
    }
    int cnt=0;
    string sfrount="";
    for(int i=s1.length()-1;i>=0;i--)
    {
        sfrount+=s1[i];
        cnt++;
        if(cnt==3)
        {
            cnt=0;
            
        sfrount+=',';
        }
        
    }
    reverse(sfrount.begin(), sfrount.end());
    if(s1.length()%3==0)
    {
        sfrount.erase(0, 1);
    }
    if(s[0]=='-')
    {
        // s1.erase(0, 1);
    cout<<"(";
    }
    cout<<"$";
    if(sfrount!="")
    {
    cout<<sfrount;
    }
    else{
        cout<<"0";
    }
    cout<<".";
    if(s2=="")
    {
        // cout<<"hi";
        cout<<"00";
    }
    
    cout<<s2;
    if(s[0]=='-')
    {
        cout<<")"<<endl;
    }
    // cout<<"hi";
    // cout<<s2<<"hi";
    
}

int main()
{
    ll i = 1;
    // cin>>i;
    while (i > 0)
    {
        solve();
        i--;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster