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张卡片中数值使其为零至少要取多少次。卡片数值的绝对值不超过x,卡片无数。
题解:这n张卡片数值绝对值除以X。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
//#define DEBUG
int main()
{
#ifdef DEBUG
freopen("cin.txt", "r", stdin);
freopen("cout.txt", "w", stdout);
#endif
int n,x,i;
while (~scanf("%d%d",&n,&x))
{
int s=0,a;
for (i=1;i<=n;i++)
{
scanf("%d",&a);
s+=a;
}
if (s<0) s=0-s;
if (s==0) printf("0\n");
else
if (s<=x) printf("1\n");
else
if (s % x==0) printf("%d\n",s/x);
else printf("%d\n",s/x+1);
}
return 0;
}
本文介绍了一个有趣的问题:如何通过最少次数找到特定数量的卡片使卡片数值总和为零。讨论了输入输出格式、示例测试及解决方案。
315

被折叠的 条评论
为什么被折叠?



