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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章