题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6)
AAAAAccepted code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int a,b; 5 cin>>a>>b; 6 int ans=a+b; 7 if(ans<0){ 8 cout<<"-"; 9 ans=-ans; 10 } 11 int x=ans%1000; 12 int y=ans/1000; 13 int z=y%1000; 14 int yy=y/1000; 15 if(yy){ 16 cout<<yy<<","; 17 if(z/100) 18 cout<<z<<","; 19 else if(z/10) 20 cout<<"0"<<z<<","; 21 else if(z) 22 cout<<"00"<<z<<","; 23 else 24 cout<<"000,"; 25 if(x/100) 26 cout<<x; 27 else if(x/10) 28 cout<<"0"<<x; 29 else if(x) 30 cout<<"00"<<x; 31 else 32 cout<<"000"; 33 } 34 else if(z){ 35 cout<<z<<","; 36 if(x/100) 37 cout<<x; 38 else if(x/10) 39 cout<<"0"<<x; 40 else if(x) 41 cout<<"00"<<x; 42 else 43 cout<<"000"; 44 } 45 else 46 cout<<x; 47 return 0; 48 }