1001. A+B Format (20)

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).


//此代码有两个测试点未过
#include
using namespace std;
#include


int meas(int n){
  int i=1;
  while(n/10){
    i++;
    n=n/10;
  }
  return i+(i-1)/3;
}

int main(){
  int a,b;
  //while(true){
    //cout<<"put";
    cin>>a>>b;
    int c=a+b;
    int check;
    if(c>0)check=1;
    else check=0;
    c=abs(c);

    if(c/1000>0){
      int spa=meas(c);
      //cout<<"spa"<<spa<<" c:"<<c<<endl;
      char* res=new char[spa];
      int i=spa-1;
      while(i>=0){
        if((spa-i)%4==0){
          res[i]=',';
        }else{
          res[i]=c+'0';
          c=c/10;
        }
        //cout<<i<<":"<<res[i]<<endl;
        i--;
      }
      if(check==0)cout<<"-";
      for(i=0;i
        cout<<res[i];
      }
    }else{
      cout<<c;
    }
    //system("pause");
  //}
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值