学堂 学堂 学堂公众号手机端

PAT A1001 A+B Format

lewis 1年前 (2024-04-28) 阅读数 17 #技术


1001A+B Format(20 分)

Calculatea+band 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 integersaandbwhere−106≤a,b≤106. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum ofaandbin one line. The sum must be written in the standard format.

Sample Input:
-1000000 9
Sample Output:
-999,991
#include<iostream>
#include<cstdio>
#include <cstring>
using namespace std;
int main(){
int a,b,c;
char str[10],str2[10];
cin>>a>>b;
c=a+b;
sprintf(str,"%d",c);
int len=strlen(str);
int index=0,k=0;
for(int i=len-1;i>=0;i--){
if(k!=0&&k%3==0&&!(i==0&&!isdigit(str[i]))) str2[index++]=',';
str2[index++]=str[i];
k++;
}
for(int i=index-1;i>=0;i--){
cout<<str2[i];
}
cout<<endl;
return 0;
}

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门