CF428 (2) A. Arya and Bran

来源:互联网 发布:泰国地图导航软件 编辑:程序博客网 时间:2024/05/16 17:01

A. Arya and Bran

                  time limit per test1 second                  memory limit per test256 megabytes                       inputstandard input                      outputstandard output  Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.  At first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later.  Your task is to find the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).Print -1 if she can't give him k candies during n given days.InputThe first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10000).The second line contains n integers a1, a2, a3, ..., an (1 ≤ ai ≤ 100).OutputIf it is impossible for Arya to give Bran k candies within n days, print -1.Otherwise print a single integer — the minimum number of days Arya needs to give Bran k candies before the end of the n-th day.ExamplesInput2 31 2Output2Input3 1710 10 10Output3Input1 910Output-1NoteIn the first sample, Arya can give Bran 3 candies in 2 days.In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.In the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day.

大致的题意:Arya每天给Bran糖果,且每天最多只能给8颗,没有给完的的剩余的糖果可以保存下来,问最少需要多少天能给k颗糖给Bran?如果不能满足就输出-1

好久都没有敲代码了,题目读懂了,但是题意没有完全理解,wa了好多发呢!!!!!最近都在学Java,打算练习Java


代码写得不是很规范,以及有些东西可能没有注意,希望大家能指正

import java.util.*;public class Main{    public static void main(String arges[]){        Scanner in = new Scanner(System.in);        int n, k;        n = in.nextInt();        k = in.nextInt();        int s = 0;        for (int i = 1; i <= n; i++){            int t = in.nextInt();            s += t;            k -= min(s, 8);            s -= min(s, 8);            if (k <= 0){                System.out.println(i);                return ;            }        }        System.out.println(-1);        in.close()    }    public static int min(int a, int b){        return a > b ? b : a;    }}