本文共 1135 字,大约阅读时间需要 3 分钟。
要解决这个问题,我们需要确定是否可以通过每次修剪n-1朵花,使得所有花的高度最终相同。如果可以实现,就输出需要的次数,否则输出-1。
#include#include using namespace std;int a[100005], n, sum, i;int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { cin >> n; sum = 0; for (i = 0; i < n; i++) { a[i] = cin >> i; } sort(a, a + n); int max_val = a[n - 1]; for (i = 0; i < n - 1; i++) { sum += (max_val - a[i]); } if (sum % (n - 1) != 0) { cout << -1; } else { int count = sum / (n - 1); cout << count; } }}
通过这种方法,我们可以有效地判断是否可以通过每次修剪n-1朵花,使得所有花的高度相同,并输出需要的次数或-1。
转载地址:http://ytwzz.baihongyu.com/