[ create a new paste ] login | about

Link: http://codepad.org/3XhHtMWx    [ raw code | output | fork ]

stimim - C++, pasted on Apr 4:
#include<iostream>
using namespace std;

const double prices[] = 
   { 55.39, 109.23, 48.29, 81.59, 81.58, 105.53, 94.45, 12.24 }; 

bool mark[8] ;
const int size = 8 ;
const char * act[] = {"\tbuy 1" , "\tsell all" , "\tpass"} ;

int main() {
   for (int i = size - 1 , v = size - 1 ; ~i ; -- i)
      if (prices[i] >= prices[v])
         mark[v = i] = true ;
   double cost = 0.0 , earned = 0.0;
   int ntickets = 0 ;
   for (int i = 0 ; i < size ; ++ i) {
      if (mark[i]) {
         if (ntickets) {
            earned += ntickets * prices[i] - cost ;
            ntickets = 0 ;
            cost = 0 ;
            cout << prices[i] << act[1] << endl ;
         } else cout << prices[i] << act[2] << endl ;
      } else {
         cout << prices[i] << act[0] << endl ;
         cost += prices[i] ;
         ntickets ++ ;
      }
   }

   cout << endl << "Totally earned: " << earned << endl ;
   return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
55.39	buy 1
109.23	sell all
48.29	buy 1
81.59	buy 1
81.58	buy 1
105.53	sell all
94.45	pass
12.24	pass

Totally earned: 158.97


Create a new paste based on this one


Comments: