码迷,mamicode.com
首页 > 其他好文 > 详细

PTA 1001 A+B Format

时间:2020-02-12 10:51:16      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:turn   text   base   col   ios   dig   spec   ret   specific   

问题描述:

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).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9
 

Sample Output:

-999,991

 

没啥难的,水一篇罢了,帮狗子同学改的代码,缩进没改。

 


代码:

 

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 int main()
 5 {
 6  int a,b,sum=0;
 7  long x;
 8  char num[30]={};
 9  cin>>a>>b;
10  x=a+b;
11  if (x>0){
12  while (x!=0)
13  {
14   num[sum]=x%10+48;
15   x=x/10;
16   sum++;
17  }
18  for (int i=sum-1;i>=0;i--)
19  {
20   cout<<num[i];
21   if (i%3==0&&i!=0)
22    cout<<",";
23  }}
24  else if (x==0)
25   cout<<"0";
26  else
27  {
28   while (x!=0)
29  {
30   num[sum]=abs(x%10)+48;
31   x=x/10;
32   sum++;
33  }
34  cout<<"-";
35  for (int i=sum-1;i>=0;i--)
36  {
37   cout<<num[i];
38   if (i%3==0&&i!=0)
39    cout<<",";
40  }}
41  return 0;
42 }

 

PTA 1001 A+B Format

标签:turn   text   base   col   ios   dig   spec   ret   specific   

原文地址:https://www.cnblogs.com/jarvis-yang/p/12297822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!