Java 8-153个字符
高尔夫功能:
import java.util.stream.*;
boolean f(int t, IntStream s){int r=1;try{s.reduce(0,(a,b)->(a+b>=t)?(a+b):(a/(a-a)));}catch(Exception e){r=0;}return r==1;}
取消高尔夫:
import java.util.stream.*;
boolean f(int t, IntStream s) {
int r=1;
try {
s.reduce(0,(a,b) -> (a+b>=t) ? (a+b) : (a/(a-a)));
} catch(Exception e) {
r=0;
}
return r==1;
}
驱动程序:
import java.util.stream.*;
import java.util.*;
public class A {
// function f as above
public static void main(String... args) {
int t = -5;
IntStream s = null;
s = Arrays.asList(-6).stream().mapToInt(i->i);
System.out.println(new A().f(t,s));
s = Arrays.asList(1,2,3,-20).stream().mapToInt(i->i);
System.out.println(new A().f(t,s));
s = Arrays.asList(200,-300,1000).stream().mapToInt(i->i);
System.out.println(new A().f(t,s));
System.out.println("above false, below true");
s = IntStream.empty();
System.out.println(new A().f(t,s));
s = Arrays.asList(4,-3,-6).stream().mapToInt(i->i);
System.out.println(new A().f(t,s));
s = Arrays.asList(-5).stream().mapToInt(i->i);
System.out.println(new A().f(t,s));
}
}
输出:
bash-3.2$ javac A.java ; java A
false
false
false
above false, below true
true
true
true