PAT (Advanced Level) Practice 1001 A+B Format

版权声明:copyright©CodeIover reserved https://blog.csdn.net/qq_40073459/article/details/86550428

                                   1001 A+B Format

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 −10​6​​≤a,b≤10​6​​. 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

running code:

#include<cstdio> 
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    int sum=a+b,res[10],len=0;
    if(sum<0)
    {
    	printf("-");
    	sum=-sum;
    }
    if(sum==0) res[len++]=0;
    while(sum)
    {
       res[len]=sum%10;
       sum/=10;
       len++;
     }
    for(int j=len-1;j>=0;j--)
      {
	 printf("%d",res[j]);
         if(j>0&&j%3==0) printf(",");
      }	
     return 0;
}

result:

猜你喜欢

转载自blog.csdn.net/qq_40073459/article/details/86550428
今日推荐