PAT (Basic Level) 1012 数字分类

题意

给定一个数组,按5的剩余系分类,再按要求输出信息。

思路

模拟即可。用了一些有趣的函数,有兴趣可以自学一下哦:cppreference

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	vector<vector<int>> a(5);
	for (int i = 0, x; i < n; ++i) {
		cin >> x;
		if (x % 5 == 0) {
			if (x % 10 == 0) a[x % 5].push_back(x);
			continue;
		}
		a[x % 5].push_back(x);
	}
	if (a[0].size() == 0) cout << "N ";
	else cout << accumulate(a[0].begin(), a[0].end(), 0) << ' ';
	if (a[1].size() == 0) {
		cout << "N ";
	}
	else {
		int sum = 0;
		for (int i = 0, tmp = 1; i < a[1].size(); ++i, tmp *= -1) 
			sum += tmp * a[1][i];
		cout << sum << ' ';	
	}
	if (a[2].size() == 0) cout << "N ";
	else cout << a[2].size() << ' ';
	if (a[3].size() == 0) cout << "N ";
	else cout << fixed << setprecision(1) << 1.0 * accumulate(a[3].begin(), a[3].end(), 0) / a[3].size() << ' ';
	if (a[4].size() == 0) cout << "N\n";
	else cout << *max_element(a[4].begin(), a[4].end()) << '\n';
	return 0;
} 

HINT

不定时更新更多题解,Basic Level 全部AC代码,详见 link ! ! !

发布了31 篇原创文章 · 获赞 15 · 访问量 768

猜你喜欢

转载自blog.csdn.net/abcdefbrhdb/article/details/104563607