PAT 1001 A+b Format

Source: Internet
Author: User

1001 A+b Format (title address?)

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

Input Specification:

Each input file contains the one test case. Each case contains a pair of integersA andb where?10? 6 ?? ≤a,b ≤10 ?6??. The numbers is separated by a space.

Output Specification:

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:
-10000009

Sample Output:
-999,991

The subject is to enter a A, a two number (one row), the output and a+b, and to be formatted in the form of output, and to be able to test multiple sets of data (a simple list of several and the case):

1 122 1,234-1-123,123

Ideas and AC code:

The key to the topic of AC is how to solve the problem of formatted output, and this reminds me of the idea of using the stack is very consistent with the requirements of the problem, the a+b and from the low into the stack, and finally in the stack to determine the number of the output comma after.

Points to note:

1 How to insert a comma in a multi-digit number: The idea of a stack or an array is used to break it down. 2 to judge the output of the first comma. 3 . Attention and positive and negative conditions 4. It's best not to export commas after a number.

AC Code 1:

#include <iostream>using namespacestd;intMain () {intA, B, c[Ten],top;  while(Cin >> a >>b) {top= -1;//Initialize TopA = a +b; if(A <0)//take a+b and the absolute value{cout<<"-"; A= -A; }    if(A = =0)//and for the situation of 0cout <<"0";  while(a)//pair and make redundancy one by one into the stack{c[++top] = a%Ten; A= A/Ten; }    intLength = top +1; intK = length%3;//the token used to record the first comma (for example, five digits will be output in the second bit, 4 digits is the first output)     while(Top >=0)//loop out the stack{cout<< c[top--]; if(Length >3&& Top >=2)//judgment and less than or equal to three digits do not output commas, and ensure that the last comma output is output at the third count            if(Length-top-1= = K | | (Length-top-k-1)%3==0))//to the left is to judge the first comma output, the right is to determine the remaining comma output (that is, 3 redundancy)cout <<","; } cout<<"\ n"; }  return 0;}

AC Code 2: Given the scope can be directly written out in the scope of the situation, the code is relatively concise and efficient, and easy to understand here is not much explanation. Of course I am using the above code AC crying smiley ...

#include <iostream>using namespacestd;intMain ()
{ intb; CIN>>a>>b; intc=a+b; if(c<0) {cout<<'-'; c=-C;} if(c>=1000000) {printf ("%d,%03d,%03d", c/1000000, c%1000000/ +, c% +); }Else if(c>= +) {printf ("%d,%03d", c/ +, c% +); }Else{printf ("%d", c); } return 0;}

PAT 1001 A+b Format

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.