贪心
比特币最佳买卖时机
http://www.nowcoder.com/questionTerminal/08f70daa78bf45fea64d72523a3641f3
//注意:最多只能一笔交易 import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[]v = sc.nextLine().split(" "); int[] p = new int[v.length]; for(int i=0;i<v.length;++i) { p[i] = Integer.parseInt(v[i]); } int max_profit = 0; int min_index=0; for(int i=0;i<v.length;++i) { if(p[i]-p[min_index]>0) { max_profit = max(max_profit,p[i]-p[min_index]); }else{ min_index=i; } } System.out.println(max_profit); } public static int max(int... a) { return Arrays.stream(a).max().getAsInt(); } }
暴力
//注意:最多只能一笔交易 import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[]v = sc.nextLine().split(" "); int[] p = new int[v.length]; for(int i=0;i<v.length;++i) { p[i] = Integer.parseInt(v[i]); } int max=0; for(int i=p.length-1;i>0;--i) { for(int j=i-1;j>=0;--j) { max = max(p[i]-p[j],max); } } System.out.println(max); } public static int max(int... a) { return Arrays.stream(a).max().getAsInt(); } }