Pat Grade-1001. A+b Format (20) Comma delimited

Source: Internet
Author: User
Tags strlen time limit
1001. A+b Format (+)Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue

Calculate A + B and output the sum in standard format – that's, the digits must be separated to groups of three by COM Mas (unless there is less than four digits).

Input

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

Output

For each test case, you should output the sum of A and B on one line. The sum must is written in the standard format. Sample Input

-1000000 9
Sample Output
-999,991

Calculates the value of the a+b, separating each 3 digits of the result with a comma.

#include <bits/stdc++.h> using namespace std; 
    #define MAXN 1000010 #define INF 0xfffffff int main () {#ifdef Online_judge #else freopen ("F:/cb/read.txt", "R", stdin);
Freopen ("F:/cb/out.txt", "w", stdout);
    #endif Ios::sync_with_stdio (FALSE);
    Cin.tie (0);
    int a,b,ans,i,j,cnt=0;
    BOOL flag=true;//positive and negative sign cin>>a>>b;
    Ans=a+b;
    cout<< "ans=" <<ans<<endl;
    if (ans<0) flag=false,ans=abs (ans);
    Char S[MAXN],C[MAXN];
        while (ans>0)//int to string {S[cnt++]=char (ans%10+ ' 0 ');
    ans/=10;
    } if (ans==0) c[0]= ' 0 ';
    cnt=0;
    For (I=strlen (s)-1; i>=0;-I.) c[cnt++]=s[i];
    cout<< "c=" <<c<<endl;
    int Len=strlen (c);
    int TEMP=LEN/3;
    if (len%3==0)--temp;//comma number//cout<< "len=" <<len<<endl;
    cnt=0;
    if (!flag) cout<< "-";
    for (i=0; i<len-3*temp; ++i) cout<<c[i];
        if (temp>0) {cout<< ",";for (j=i; j<len; ++j) {if (cnt==3) {cnt=0;
            cout<< ",";
            } cout<<c[j];
        ++cnt;
    }} cout<<endl;
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.