PAT Class --1001 A + B Format (20 minutes)

Ideas:

No visible range of the number of super-long long, it does not require a string processing;

Each vector is stored in a withdrawn or array, when the remaining number of bits is an integral multiple of 3 when the output comma;

The fourth test point 0 + 0, pay attention to consider boundary value.

1001 A + B Format (20 minutes)

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

 

//1001
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

int main(){
	
	ll a,b;
	cin>>a>>b;
	ll c =a+b;
	if(c<0){
		cout<<"-";
	}
	if(c==0){
		cout<<0;
	}
	c=abs(c);
	vector<int> s;
	int d;
	while(c>0){
		d=c%10;
		s.push_back(d);
		c=c/10;
	}
	int k=s.size();
	for(int i=k-1;i>=0;i--){
		if(i%3==2&&i!=k-1){
			cout<<",";
		}
			
		cout<<s[i];
	}
	cout<<endl;
		
	return 0;
}

 

Published 234 original articles · won praise 216 · Views 100,000 +

Guess you like

Origin blog.csdn.net/qq_41895747/article/details/104070527