PAT (Advanced Level) Practice 1001 A+B Format (20) C++

本文介绍了一个使用 C++ 实现的大数输出程序,该程序能够根据输入的两个整数之和的不同范围,采用不同的格式进行输出。具体包括处理正数、负数以及特殊边界条件的情况。

本题主要考察边界条件判断,数字取模取余运算。

#include <iostream>
#include <cstdio>
#include <string>
#include<iomanip>
using namespace std;
int main(void)
{
	long n,m;
	cin >> n >> m;
	long ad = n + m;	
	string s;
	if (ad >= 1000000) {
		cout << ad / 1000000 << "," << right << setw(3) << setfill('0')<< ad/1000%1000 << "," << right << setw(3) << setfill('0') <<ad % 1000;
	}
	else if (ad <= -1000000) {
		ad = -ad;
		cout <<"-"<< ad / 1000000 << "," << right << setw(3) << setfill('0') << ad / 1000 % 1000 << "," << right << setw(3) << setfill('0') << ad % 1000;
	}
	else if (-1000>=ad ) {
		ad = -ad;
		cout<<"-"<<ad/1000<<","<< right << setw(3) << setfill('0') << ad % 1000;
	}
	else if (ad >= 1000) {
		cout << ad / 1000 << "," << right << setw(3) << setfill('0') << ad % 1000;
	}
	else {
		cout << ad;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值