princennamdi

Stock(with indexes) [java]

Nov 15th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.66 KB | None | 0 0
  1. class stock{
  2.     public stock (int[] num){
  3.         int least = 0;
  4.         int profit = 0;
  5.         int index = 0;
  6.         int end = 0;
  7.         int x = num.length;
  8.        
  9.         for (int i=0; i <x; i ++){
  10.             profit += num[i];
  11.            
  12.             if (least>profit){
  13.                     least=profit;
  14.                     end=i;}
  15.             else{
  16.                 if (profit >= 0){
  17.                     profit=0;
  18.                     index= i+1;
  19.                 }}}
  20.        
  21.         System.out.print(least + " " + index + " " + end);
  22.     }
  23.  
  24.     public static void main(String[] args){
  25.         //insert the stock values here or change below to input statement...
  26.         int[] w = {-9,-9,-7,6,-8,6,-7,-5,2,-6,0,-2,5,4,-5};  
  27.         stock b = new stock(w);
  28.     }}
Advertisement
Add Comment
Please, Sign In to add comment