Server: Difference between revisions
Jump to navigation
Jump to search
imported>Kmk21 No edit summary |
imported>Kmk21 No edit summary |
||
Line 37: | Line 37: | ||
[[Category:rmrc2014]] | [[Category:rmrc2014]] | ||
[[Category:ncna2014]] | |||
[[Category:ICPC Problems]] | [[Category:ICPC Problems]] | ||
[[Category:Algorithm Trivial]] | [[Category:Algorithm Trivial]] | ||
[[Category:Implementation Trivial]] | [[Category:Implementation Trivial]] |
Latest revision as of 00:30, 17 February 2015
Introduction
This problem gives you an array of numbers, and asks you the largest n such that the first n elements sum to less than t.
Solutions
Iterate and sum
Idea
Walk through the array adding the numbers until the sum is greater than t. Print out the index.
Runtime
- n
Gotchas
Be sure to handle the case when all the numbers sum to less than t, or some n numbers sum to exactly t.
Code
Solution - Java
import java.util.*;
public class a {
Scanner in=new Scanner(System.in);
public static void main(String[] args) {
new a().go();
}
private void go() {
int n=in.nextInt(),t=in.nextInt(),ans=0;
while(t>0&&n-->0){
t-=in.nextInt();
if(t<0)break;
ans++;
}
System.out.println(ans);
}
}