HDOJ 2401 Baskets of Gold Coins

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mml5211314/article/details/48421643

Baskets of Gold Coins


Problem Description
You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes 1 coin from Basket 1, 2 coins from Basket 2, and so on, up to and including N-1 coins from Basket N-1. He does not take any coins from Basket N. He weighs the selected coins and concludes which of the N baskets contains the lighter coins. Your mission is to emulate the wizard's computation.

Input
The input file will consist of one or more lines; each line will contain data for one instance of the problem. More specifically, each line will contain four positive integers, separated by one blank space. The first three integers are, respectively, the numbers N, w, and d, as described above. The fourth integer is the result of weighing the selected coins.
N will be at least 2 and not more than 8000. The value of w will be at most 30. The value of d will be less than w.

Output
For each instance of the problem, your program will produce one line of output, consisting of one positive integer: the number of the basket that contains lighter coins than the other baskets.

Sample Input
 
  
10 25 8 1109 10 25 8 1045 8000 30 12 959879400

Sample Output
 
  
2 10 50
 

//这道题的题意是有N个篮子,篮子里面装有硬币,每个硬币的重量是w,但是有一篮子中,里面的硬币重b,有一个巫师从第一个篮子里开始拿硬币,第一个篮子拿一个,第二个篮子拿两个,第三个篮子拿三个,,,,,,第N-1个篮子拿N-1个,最后一个篮子不拿,给出所拿硬币的从重量,求哪个篮子的重量最小。
//算法分析:先求1到N-1个篮子的总重量,即等差数列的求和公式,如果和等于sum,则最轻的为第N个篮子,若不相等,和减去sum,再除去d,得最轻篮子
#include<cstdio>
#include<cstring>
int main()
{
	int N,w,d,sum;
	while (scanf ("%d%d%d%d",&N,&w,&d,&sum)!=EOF)
	{
		int sum1;
		sum1=(N-1)*N*w/2;
		//sum2=sum1-sum;
		if (sum1==sum)
		printf ("%d\n",N);
		else
		printf ("%d\n",(sum1-sum)/d);
	}
	return 0;
}


















猜你喜欢

转载自blog.csdn.net/mml5211314/article/details/48421643