PAT (Advanced Level) T1001 A+B Format

link

题目描述

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).

输入描述

Each input file contains one test case. Each case contains a pair of integers a and b where −106 ≤ a,b ≤ 106. The numbers are separated by a space.

输出描述

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.

样例

-1000000 9
-999,991

思路

题目就是让按照外国那种3位一个,的方式输出两个数相加的结果,然后就自然而然想到了manacher里插字符的骚操作,所以我干脆也插字符了
注意:-,999,991是错的(不用想也知道

实现

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

int a,b,cnt,t;
char s[15];

inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}

void fun(int x){
	int f=0;//判断是否应该加','
	if(!x){
		putchar('0');
		return;
	}
	if(x<0) putchar('-'),x=-x;
	while(x){
		if(t%3==0&&f) s[cnt++]=',';
		s[cnt++]=x%10+'0';
		t++;//和cnt区分开,防止','多占位数
		x/=10;
		f=1;
	}
	for(int i=cnt-1;i>=0;i--){
		putchar(s[i]);
	}
}

int main(){
	a=read(),b=read();
	fun(a+b);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值