PAT Practice 1

#include <bits/stdc++.h>
using namespace std;

int main () {
	int a, b;
	cin >> a >> b;
	a += b;
	stack<int> s;
	bool flag = 0;
	while(abs(a) > 999) {
		s.push(abs(a) % 1000);
		a /= 1000;
		flag = 1;
	}	
	cout << a;
	if(flag) {
		cout << ",";
	}
	int cap = s.size();
	for(int i = 0; i < cap; ++i) {
		// cout << i << " 88888" << s.top() << endl;

		if(s.top() < 10) {
			cout << "00";
		}
		else if(s.top() < 100) {
			cout << "0";
		}
		cout << s.top();

		if(i != cap - 1) {
			cout << ',';
		} 
		s.pop();
 	}
 	cout << endl;
 	return 0;
}

 

 也可以用数组

#include <bits/stdc++.h>
using namespace std;
int main () {
	map<int, double, greater<int>> mp;
	int a, b;
	int num = 0;
	cin >> a;
	while(a--) {
		int s; double d;
		cin >> s >> d;
		mp[s] += d;
	}
	cin >> b;
	while(b--) {
		int s; double d;
		cin >> s >> d;
		mp[s] += d;
	}
	map<int, double, greater<int>> :: iterator it = mp.begin();
	for(it = mp.begin(); it != mp.end(); it++) {
		if(it->second != 0) {
			num++;
		}
	}

	cout << num;
	// map<int, double, greater<int>> :: iterator it = mp.begin();
	for(it = mp.begin(); it != mp.end(); it++) {
		if(it->second != 0) {
			printf(" %d %.1f", it->first, it->second);
		}
	}
	cout << endl;
}
作者:LightAc
出处:https://www.cnblogs.com/lightac/
联系:
Email: dzz@stu.ouc.edu.cn
QQ: 1171613053
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/lightac/p/12957602.html