Pat_1001(甲级) | StriveZs的博客

Pat_1001(甲级)

1001 A+B Format (20 分) 原文地址

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

代码:(这里自己写过一个可以使所有数据进行相加减的大数运算 但是会发生time out 因此这个参考了网上大佬写的,基本上能看懂所以在这里就贴大佬的了)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
int c=a+b;
if(c<0) cout<<'-';
c=abs(c);
char s\[20\];
sprintf(s,"%d",c);
int len=strlen(s);
//if(len<=3) {cout<<s;return 0;}
int m=len/3,n=len%3,start=0;
//cout<<"m="<<m<<' '<<"n="<<n<<endl;
if(n==0) {cout<<s\[0\]<<s\[1\]<<s\[2\];start=3;m--;}
else if(n==1) {cout<<s\[0\];start=1;}
else if(n==2) {cout<<s\[0\]<<s\[1\];start=2;}
while(m!=0){
cout<<',';
cout<<s\[start\]<<s\[start+1\]<<s\[start+2\];
start+=3;
m--;
}
return 0;
}
StriveZs wechat
Hobby lead  creation, technology change world.
  • Post author: StriveZs
  • Post link: 1816.html
  • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.