CodeForces 727B BillTotalValue 字符串模拟题

//http://codeforces.com/problemset/problem/727/B

//stringstream转double会丢精度,改为用sprintf(Str,"%f",Ans)来转double;

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e3+5;
char Str[MAX];
double Ans=0;
int main()
{
    cin>>Str;
    int len=strlen(Str);
    for (int i=0,j,k,num_dots,pos_lastdot; i<len; i++)
    {
        if (isdigit(Str[i])||Str[i]=='.')
        {
            char temp[MAX]= {0};
            for (j=i,k=0,num_dots=0,pos_lastdot=0; isdigit(Str[j])||Str[j]=='.'; j++)
                if (Str[j]=='.')
                    num_dots++,pos_lastdot=j;
                else
                    temp[k]=Str[j],k++;
            //cout<<temp<<' '<<k<<' '<<num_dots<<' '<<j-i<<' '<<pos_lastdot<<' '<<j<<endl;
            stringstream ss(temp);
            int num;
            ss>>num;
            if (j-pos_lastdot==3&&num_dots)
                Ans+=0.01*num;
            else if (j-pos_lastdot==2&&num_dots)
                Ans+=0.1*num;
            else
                Ans+=num;
            i=j;
        }
    }
    sprintf(Str,"%f",Ans);
    len=strlen(Str);
    for (int i=len-1; i>=0; i--)
        if (Str[i]=='0')
            Str[i]='\0';
        else
            break;
    len=strlen(Str);
    bool Mark[MAX]= {false};
    int pos;
    for (int i=len-1; i>=0; i--)
        if (Str[i]=='.')
        {
            pos=i;
            for (int j=i; j>0; j-=3)
                Mark[j]=true;
            if (i==len-2)
                Str[len]='0',Str[len+1]='\0';
            if (i==len-1)
                Str[i]='\0';
            break;
        }
    len=strlen(Str);
    for (int i=0; i<len; i++)
    {
        if (Mark[i])
            cout<<'.';
        if (i!=pos)
            cout<<Str[i];
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值