练习cf1234A. Equalize Prices Again
题目如下
A. Equalize Prices Again
time limit per test1 second
memory limit per test256 megabytes
You are both a shop keeper and a shop assistant at a small nearby shop. You have 𝑛 goods, the 𝑖-th good costs 𝑎𝑖 coins.
You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all 𝑛 goods you have.
However, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all 𝑛 goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.
On the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one.
So you need to find the minimum possible equal price of all 𝑛 goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.
You have to answer 𝑞 independent queries.
Input
The first line of the input contains one integer 𝑞 (1≤𝑞≤100) — the number of queries. Then 𝑞 queries follow.
The first line of the query contains one integer 𝑛 (1≤𝑛≤100) — the number of goods. The second line of the query contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤107), where 𝑎𝑖 is the price of the 𝑖-th good.
Output
For each query, print the answer for it — the minimum possible equal price of all 𝑛 goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.
题目大意
现有价格不同的n个商品,现在要将他们的价格统一,但是总价不能少于原来的总价。
题目分析
取平均值,再验证,少于再加一。
点击查看代码
#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
long long price[101];
unsigned long long sum = 0;
long long new_price;
for(int i = 0; i < n; i++){
cin >> price[i];
sum += price[i];
}
new_price = sum / n;
if(new_price * n < sum){
new_price += 1;
}
cout << new_price << endl;
}
return 0;
}

浙公网安备 33010602011771号