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

1001. A+B Format

时间:2015-01-26 22:54:20      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:c++   pat   

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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Draw (char *str,int a,int b);
int main ()
{
    long a,b;
    scanf("%ld %ld",&a,&b);
    long c=a+b;
    char str[9];
    sprintf(str,"%d",c);
    int length=strlen(str);
    if( str[0]-'-'==0)
    {
        printf("-");
        Draw(str,1,length-1);
        }
    else Draw(str,0,length-1);
    printf("\n");
    system("pause");
    return 0;
    }
void Draw (char *str,int a,int b)
{
     int length=b-a+1;
     int c=length%3,i;
     for (i=0;i<c;i++) printf("%c",str[i+a]);
     while( i<length) 
     {
            if( i!=0) printf(",");
            printf("%c",str[i+a]);
            i++;
            printf("%c",str[i+a]);
            i++;
            printf("%c",str[i+a]);
            i
            ++;
            }
     }

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.

1001. A+B Format

标签:c++   pat   

原文地址:http://blog.csdn.net/lchinam/article/details/43157783

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