# 双胞胎## 题目描述小龙在柜子里面藏了 n 种不同的零食每一种零食有 $bi$ 个小龙的双胞胎弟弟小虎发现了小龙的零食要求小龙平分。小龙想知道自己还需要买多少零食才能把这些零食分成一模一样的两份。## 输入格式第一行为一个整数 n$nleq 10^5$表示零食的种类数。第二行为 n 个整数表示每一种零食的个数每个整数之间用一个空格隔开。## 输出格式输出一个整数表示小龙还需要买的零食的个数。#
Python3 代码
n = int(input())
snacks = list(map(int, input().split()))
total = sum(snacks)
if total % 2 == 1:
print(-1)
else:
target = total // 2
count = 0
for snack in snacks:
if snack > target:
print(-1)
break
elif snack == target:
count += 1
else:
count += 2
else:
print((count - n) // 2)
C++ 代码
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> snacks(n);
int total = 0;
for (int i = 0; i < n; i++) {
cin >> snacks[i];
total += snacks[i];
}
if (total % 2 == 1) {
cout << -1 << endl;
} else {
int target = total / 2;
int count = 0;
for (int i = 0; i < n; i++) {
if (snacks[i] > target) {
cout << -1 << endl;
return 0;
} else if (snacks[i] == target) {
count++;
} else {
count += 2;
}
}
cout << (count - n) / 2 << endl;
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/fblo 著作权归作者所有。请勿转载和采集!