谁能帮忙加个高精
查看原帖
谁能帮忙加个高精
1377891
lly66666楼主2024/11/21 20:07
#include <bits/stdc++.h>
using namespace std;
int a[10005], f[10005];
int main() {
	while(1) {
		int n, m;
		cin >> n >> m;
		for(int i = 1; i <= m; i ++) f[i] = 1;
		if(n == 0 && m == 0) break;
		int zheng = 0, fu = 0, ling = 0, zhengs[10005] = {0}, fus[10005] = {0}, lings[10005] = {0}, ans = 0;
		for(int i = 1; i <= n; i ++) {
			cin >> a[i];
			if(a[i] > 0) zhengs[++ zheng] = a[i];
			if(a[i] == 0) lings[++ ling] = a[i];
			if(a[i] < 0) fus[++ fu] = a[i];
		}
		if(m == 1 && ling > 0) {
			cout << 0 << endl;
			continue;
		}
		if(m > 1) {
			for(int i = 1; i <= zheng; i ++) {
				f[1] *= zhengs[i];
			}
			if(ling > 0) f[2] = 0;
			sort(fus + 1, fus + fu + 1);
			if(fu % 2 == 0) {
				for(int i = 1; i <= fu; i ++) f[1] *= abs(fus[i]);
			}
			else {
				for(int i = 1; i < fu; i ++) f[1] *= fus[i];
				f[2] *= fus[fu];
			}
			for(int i = 1; i <= m; i ++) ans += f[i];
		}
		else {
			for(int i = 1; i <= n; i ++) f[1] *= a[i];
			ans = f[1];
		}
		cout << ans << endl;
	}
	return 0;
}

感觉好像得加个高精度才能过

哪名巨佬能帮个忙

思路 -- 贪心

2024/11/21 20:07
加载中...