Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.
Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?
You can assume that initially Vanya had infinitely many cards with each integer number from - x to x.
The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.
Print a single number — the answer to the problem.
3 2 -1 1 2
1
2 3 -2 -2
2
In the first sample, Vanya needs to find a single card with number -2.
In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.
题意:
一共有 N(1 ~ 1000) 张牌,每张牌上面的点数绝对值不超过X (1 ~ 1000),后给出这 N 张牌是什么,求最多要增加多少张牌,使所有点数之和等于 0 。
思路:
贪心。
AC:
#include <stdio.h> int main() { int n,x,sum = 0; scanf("%d%d",&n,&x); while(n--) { int num; scanf("%d",&num); sum += num; } if(!sum) printf("0\n"); else { if(sum < 0) sum = -sum; if(sum < x) printf("1\n"); else { if(!(sum % x)) printf("%d\n",sum / x); else printf("%d\n",sum / x + 1); } } return 0; }
相关推荐
此项目的开发和发行已移至GITHUB:https://github.com/vanya-antonov/assa
此项目的开发和发行已移至GITHUB:https://github.com/vanya-antonov/genetack
fbdebugger-新 使用VANYA的Window9库的新版FBdebugger也适用于Linux(并且始终适用于Windows),也使用了scintilla库。 功能将少于fbdebugger的initila版本。
克隆存储库: git clone https://github.com/Vanya9422/react-apollo-graph.git 转到服务器文件夹并安装依赖项: npm install 运行项目: npm run dev 快速入门申请 转到应用程序文件夹并安装依赖项: yarn ...
测试2 =================== Anggota Kelompok: Vanya Widya Meifarizka S(181402123)(项目经理/ CSS) Lolyvia Khoiria Daulay(181402018)(后端) Tengku Zalfa Qadriyya Munadhila(181402138)(前端)====...