PAT (Advanced Level) Practice__10001

PAT(Advanced Level) Practice__1001


1001 A+B Format (20分)
在这里插入图片描述

//计算a+b
//输出结果要求逗号分隔
//xxx,xxx,xxx
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stdlib.h>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	int sum = a + b;
	if ((sum>=0&&sum<1000)||(sum>-1000&&sum<=0))
	{
		cout << sum;
	}
	else
	{
		int index = sum % 1000;
		int index2 = sum / 1000;
		int index3 = sum % 1000000;
		int index4 = sum / 1000000;
		if (index <= 0)
		{
			index = -index;
		}
		if ((sum>=1000&&sum<1000000)||(sum>-1000000&&sum<=-1000))
		{
			if (index >= 100)
				cout << index2 << "," << index;
			else if (index >= 10)
				cout << index2 << ",0" << index;
			else if (index >= 0)
				cout << index2 << ",00" << index;
		}
		else
		{
			if (index3 < 0) 
			{
				index3 = -index3;
			}
			index2 = index3 / 1000;
			if (index2 >= 100)
			{
				if (index >= 100)
					cout <<index4<<","<< index2 << "," << index;
				else if (index >= 10)
					cout <<index4<<","<< index2 << ",0" << index;
				else if (index >= 0)
					cout <<index4<<","<< index2 << ",00" << index;
			}
			else if (index2 >= 10)
			{
				if (index >= 100)
					cout <<index4<<",0"<< index2 << "," << index;
				else if (index >= 10)
					cout <<index4<<",0"<< index2 << ",0" << index;
				else if (index >= 0)
					cout<<index4<<",0" << index2 << ",00" << index;
			}
			else if (index2 >= 0)
			{
				if (index >= 100)
					cout << index4 << ",00" << index2 << "," << index;
				else if (index >= 10)
					cout << index4 << ",00" << index2 << ",0" << index;
				else if (index >= 0)
					cout << index4 << ",00" << index2 << ",00" << index;
			}
		}

	}
}

在这里插入图片描述
在这里插入图片描述

posted @ 2021-08-19 10:23  在天边偷看小天使  阅读(7)  评论(0)    收藏  举报  来源