HDOJ 2401 Baskets of Gold Coins

来源:互联网 发布:淘宝货到付款回款周期 编辑:程序博客网 时间:2024/05/18 17:41

HDACM 2401

题目的意思:
有N个篮子,编号1—N,篮子中有很多金币,每个重w.但是有一个编号的篮子中,每个金币重w-d.女巫从第一个篮子中拿1个金币,第二个篮子中拿2个……第N-1中拿N-1个,第N中不拿,给出这些金币的总重量s,问:是第几个篮子中的金币重量较轻?
题解:
先求1—N篮子金币应有的总重量yuan=w*(1+n-1)(n-1)/2,然后求差值cha=原-s ,再除以金币重量差值d则得出轻金币的个数。若为0,则必在编号N的篮子中;若不为0,得到较轻金币的个数,即为所求编号。

import java.util.Scanner;public class Main{    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        while (sc.hasNext()) {            int n = sc.nextInt();            int w = sc.nextInt();            int d = sc.nextInt();            int x = sc.nextInt();            int sum = (n - 1) * n / 2 * w - x;            if (sum == 0) {                System.out.println(n);            }else{                System.out.println(sum/d);            }        }        sc.close();    }}
原创粉丝点击