HDOJ 2401 Baskets of Gold Coins
2015-09-13 22:06
519 查看
Baskets of Gold Coins
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
10 25 8 1109
10 25 8 1045
8000 30 12 959879400
[align=left]Sample Output[/align]
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; }
相关文章推荐
- Understanding Machine Learning: From Theory to Algorithms
- Processing实现Google新图标
- HDU 4416 Good Article Good sentence
- django学习之models2
- 排序算法 golang 实现
- POJ 2000 Gold Coins
- django学习之models1
- 【转】Google 的秘密- PageRank 彻底解说 中文版
- 【英语】Bingo口语笔记(80) - 记忆、忘记的表达
- 【英语】Bingo口语笔记(79) - fish系列
- django 1.8 官方文档翻译:2-5-9 条件表达式
- Leetcode Algorithm No.2 Add Two Numbers
- django 1.8 官方文档翻译: 8-3 点击劫持保护
- week1-10Theory of algorithms
- 谷歌新Logo如何做到只有305字节
- 谷歌新Logo如何做到只有305字节
- django复习笔记3:urls/views/templates三板斧
- Mac OS X系统搭建谷歌Go语言开发工具 Sublime Text 环境配置
- django复习笔记2:models
- uva 544 - Heavy Cargo(生成树)