void Shop::browse(Human& human) { while(shopping) { option = display->menu("Shop", itemsList); if (option == 1) { weaponsList.clear(); for (unsigned int index = 0; index < weapons.size(); ++index) { weaponsList.push_back(weapons[index].getName()); } option = display->menu("Weapon List", weaponsList); buy(human, weapons.at(--option)); } else if (option == 2) { armorList.clear(); for (unsigned int index = 0; index < armor.size(); ++index) { armorList.push_back(armor[index].getName()); } option = display->menu("Armor List", armorList); buy(human, armor.at(--option)); } else if (option == 3) { buyPotion(human); } else { shopping = false; } } }
ZR::RetVal MyOrders::match( Order * myOrder ) { if( myOrder->m_locked ) return ZR::ZR_FINISH; OrderList asks; m_asks->filterOrders( asks, myOrder->m_currency ); ZR::ZR_Number amount = myOrder->m_amount; for( OrderIterator askIt = asks.begin(); askIt != asks.end(); askIt++ ) { Order * other = *askIt; if( other->m_ignored ) continue; // trying to execute this order did not go well in the past. Don't try again. if( other->m_isMyOrder ) continue; // don't fill own orders if( myOrder->m_matched.find( other->m_order_id ) != myOrder->m_matched.end() ) continue; // matched that already if( myOrder->m_price < other->m_price ) break; // no need to try and find matches beyond std::cerr << "Zero Reserve: Match at ask price " << other->m_price.toStdString() << std::endl; myOrder->m_matched.insert( other->m_order_id ); if( amount > other->m_amount ) { buy( other, myOrder, other->m_amount ); } else { buy( other, myOrder, amount ); return ZR::ZR_FINISH; } amount -= other->m_amount; } return ZR::ZR_SUCCESS; }
int main(int argc, const char** argv) { const char* pname = argv[0]; if (argc >= 2 && is_help(argv[1])) { return usage(pname, EXIT_SUCCESS); } else if (argc >= 3 && is_help(argv[2])) { return usage(pname, EXIT_SUCCESS); } else if (argc <= 2) { error("Not enough arguments"); return usage(pname, EXIT_FAILURE); } const char* fname = argv[1]; const char* action = argv[2]; int sub_argc = argc - 3; const char** sub_argv = argv + 3; if (strncmp(action, "buy", 4) == 0) { return buy(pname, fname, sub_argc, sub_argv); } else if (strncmp(action, "sell", 5) == 0) { return sell(pname, fname, sub_argc, sub_argv); } else if (strncmp(action, "inventory", 10) == 0) { return inventory(pname, fname, sub_argc, sub_argv); } else if (strncmp(action, "state", 6) == 0) { return inventory(pname, fname, sub_argc, sub_argv); } else if (strncmp(action, "history", 8) == 0) { return history(pname, fname, sub_argc, sub_argv); } else { error("Unknown action %s", action); return usage(argv[0], EXIT_FAILURE); } }
int main() { int i,option; printf("0=buy 1=sell 2=show your store 3=exit\n"); typenum=0; for (i=1;i<=maxtype;i++) { computer[i]=0; } scanf("%d",&option); while (option!=3) { if (option==0) { buy(); } else if (option==1) { sell(); } else if (option==2) { show(); } scanf("%d",&option); } return 0; }
int maxProfit(vector<int>& prices) { int n = prices.size(); if(n <= 1) return 0; vector<int> buy(n,0); vector<int> sell(n,0); buy[0]= prices[0]; for(int i=1; i<n; i++) { if(prices[i] < buy[i-1]) buy[i] = prices[i]; else buy[i] = buy[i-1]; } sell[n-1]= prices[n-1]; for(int j=n-2; j>=0; j--) { if(prices[j] >sell[j+1]) sell[j] = prices[j]; else sell[j] = sell[j+1]; } int max = 0; for(int k=0; k<n; ++k) { if(max < sell[k]-buy[k]) max = sell[k]-buy[k]; } return max; }
/*Yapılacak operasyonların fonksiyonu*/ char operations(char ch){ switch(ch){ case 'T': case 't':printf("%c",trans(ch)); break; case 'B': case 'b':printf("%c",buy(ch)); break; case 'S': case 's':printf("%c",sell(ch)); break; case 'I': case 'i':printf("%c",inp(ch)); break; case 'O': case 'o':printf("%c",outp(ch)); break; case 'F': case 'f':printf("%c",rptfile(ch)); break; case 'C': case 'c':printf("%c",rptcnsl(ch)); break; case 'E': case 'e':printf("%c",exitt(ch)); break; } return 0; }
void on_ticket_table_row_activated( GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer userdata) { GtkTreeIter iter; GtkTreeModel *model = gtk_tree_view_get_model(treeview); gchar *id; if(gtk_tree_model_get_iter(model, &iter, path)) { gtk_tree_model_get(model, &iter, 0, &id, -1); GtkMessageDialog *dialog; if(buy(id, conn) == 0) { update_buy_table(); update_search_result(); dialog = GTK_MESSAGE_DIALOG(gtk_message_dialog_new( login_win,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_CLOSE,"Buy ticket successfully" )); } else { dialog = GTK_MESSAGE_DIALOG(gtk_message_dialog_new( login_win,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,"Buy ticket Failed" )); } gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(GTK_WIDGET(dialog)); g_free(id); } }
BuyMessage* Buyer::buy(Wallet* wallet, const vector<EncBuffer*>& ctext, const vector<hash_t>& ptHash, const ZZ &R) { startTimer(); makeCoin(*wallet, R); printTimer("[Buyer::buy] created coin"); return buy(ctext, ptHash); }
// solved by extending the method of "two transactions" // O(kn) int maxProfit(int k, vector<int>& prices) { int len = prices.size(); if(!k || len<2) return 0; if(len/2<=k){ int sum=0; for(int i=1;i<len;i++){ if(prices[i]-prices[i-1]>0) sum+=(prices[i]-prices[i-1]); } return sum; } // if k is too large, claiming vector could result runtime error vector<int> buy(k,INT_MIN), sell(k,0); for(int i=0; i<len; ++i) { // buy[j] means profit with which the last operation is buying before day i // sell[j] means profit with which the last operation is selling before day i for(int j=k-1;j>0;j--){ // compare selling on day i-1 and selling on day i(buy[j] here means j transactions buy at day i-1) sell[j]=max(sell[j],buy[j]+prices[i]); // compare buying on i-1 and buying on i buy[j]=max(buy[j],sell[j-1]-prices[i]); } sell[0]=max(sell[0],buy[0]+prices[i]); buy[0]=max(buy[0],-prices[i]); } return sell[k-1]; }
int maxProfit(vector<int>& prices) { int len = prices.size(); if ( len < 2 ) return 0; vector<int> buy(len, 0); vector<int> sell(len, 0); buy[0] = -prices[0]; int profit = 0; for (int i=1; i<len; i++) { sell[i] = max ( buy[i-1] + prices[i], sell[i-1] - prices[i-1] + prices[i] ); profit = max(profit, sell[i]); if ( i == 1) { buy[i] = buy[0] + prices[0] - prices[1]; } else { buy[i] = max ( sell[i-2] - prices[i], buy[i-1] + prices[i-1] - prices[i] ); } } return profit; }
int main() { database_init(3); init_db(); printf("Welcome to eCommerce v0.1\n"); while (1) { char choice = menu(); if (choice == 0) break; else if (choice == 'a') list(); else if (choice == 'b') buy(); else if (choice == 'c') sell(); else printf("Invalid selection\n"); } database_close(); return 0; }
int maxProfit(int k, vector<int> &prices) { int days = prices.size(); if (days < 2) { return 0; } if (k >= days / 2) { int profit = 0; for (int i = 0; i < days - 1; i++) { if (prices[i + 1] > prices[i]) { profit += prices[i + 1] - prices[i]; } } return profit; } // buy0 , sell0, buy1, sell1 vector<vector<int>> buy(k + 1, vector<int>(days, 0)); vector<vector<int>> sell(k + 1, vector<int>(days, 0)); for (int i = 1; i <= k; i++) { buy[i][0] = -prices[0]; for (int j = 1; j < days; j++) { buy[i][j] = max(buy[i][j - 1], sell[i - 1][j - 1] - prices[j]); sell[i][j] = max(sell[i][j - 1], buy[i][j - 1] + prices[j]); } } return sell[k][days - 1]; }
void Shop::renderShop() { if (shopOpen) { //Render the background lib->loop_portion(start, end, [&](Point p) { lib->rendertile(0x125, p); }); //Render the close button if (lib->mouseLoc() == start) //Highlight if mouse is over close button tl_color(0x000000FF); lib->rendertile(static_cast<int>('x'), start); tl_color(0xFFFFFFFF); //If close button is hit, close the shop if (tl_buttonwentdown(1) && lib->mouseLoc() == start) shopOpen = false; tl_scale(2); lib->renderText(Point(start.x() + 6, start.y() + 3), "Well Hello There, Stranger!"); //Compensate start point with scale lib->renderText(Point(start.x() + 10, end.y() + 12), "Gold Amount: " + to_string(shopGold)); tl_scale(1); //Render icon slots unsigned int idx = 0; Point iconStart = start + Point(5, 5); Point iconEnd = end + Point(-5, -5); lib->loop_portion(iconStart, end + Point(-5, -5), [&](Point p) { lib->rendertile(0x13D, p); if (lib->mouseLoc() == p && storeItems[idx].getLoc() != Point(-1, -1)) { tl_color(0xFF000040); lib->rendertile(0x13D, p); } tl_color(0xFFFFFFFF); if (storeItems[idx].getLoc() != Point(-1, -1)) lib->rendertile(storeItems[idx].getPickupDef().getTile(), p); idx++; }); if (lib->mouseLoc() >= iconStart && lib->mouseLoc() <= iconEnd - Point(1, 1)) { //Render item mouse over Pickup p = storeItems[idxFromPoint(lib->mouseLoc() - iconStart)]; Point loc = lib->mouseLoc() - iconStart; if (p.getLoc() != Point(-1, -1)) { if (p.getPickupDef().getType() != 0) log->renderMouseOver(p.getPickupDef().getName(), p.getPickupDef().getDESC(), "Uses left: " + to_string(p.getUsesLeft()), "Cost: " + to_string(p.getPickupDef().getPrice())); else log->renderMouseOver(p.getPickupDef().getName(), p.getPickupDef().getDESC(), "Cost: " + to_string(p.getPickupDef().getPrice()), ""); } //Buy item if (tl_buttonwentdown(1)) buy(loc); } else if (lib->mouseLoc() >= Point(lib->res().x() - 3, 3) && lib->mouseLoc() <= Point(lib->res().x() - 2, 9)) { if (tl_buttonwentdown(1)) { Pickup p = invLog->sellItem(lib->mouseLoc()); sell(p); } } } }
static int max_profit_2(int k, vector<int> &prices) { vector<int> buy(k + 1, INT_MIN); vector<int> sell(k + 1, 0); for (int i = 0; i < prices.size(); i++) { for (int j = 1; j <= k; j++) { sell[j] = max(sell[j], buy[j] + prices[i]); buy[j] = max(buy[j], sell[j - 1] - prices[i]); } } return sell[k]; }
// Read another player's move. The arbiter will always send us valid moves from the set (B, S, R, L, P) void readMove(int p) { char action; int factor, company; scanf(" %c %d %d", &action, &factor, &company); switch (action) { case 'B': buy(p, factor, company); break; case 'S': sell(p, factor, company); break; case 'R': raise(factor, company); break; case 'L': lower(factor, company); break; } }
void BuyiPhone() { while(1) { wait(t); wait(s); withdraw(); signal(s); buy(); } }
bool QmlCommerce::buy(const QString& assetId, int cost, const QString& buyerUsername) { auto ledger = DependencyManager::get<Ledger>(); auto wallet = DependencyManager::get<Wallet>(); QStringList keys = wallet->listPublicKeys(); if (keys.count() == 0) { return false; } QString key = keys[0]; // For now, we receive at the same key that pays for it. return ledger->buy(key, cost, assetId, key, buyerUsername); }
int maxProfit(vector<int>& prices) { if(prices.size() <= 1) return 0; vector<int> buy(prices.size()+1, INT_MIN); vector<int> sell(prices.size()+1, 0); buy[1] = -prices[0]; for(int i = 2; i <= prices.size(); ++i) { buy[i] = max(buy[i-1], sell[i-2] - prices[i-1]); sell[i] = max(sell[i-1], buy[i-1] + prices[i-1]); } return sell[prices.size()]; }
void QmlCommerce::buy(const QString& assetId, int cost, const bool controlledFailure) { auto ledger = DependencyManager::get<Ledger>(); auto wallet = DependencyManager::get<Wallet>(); QStringList keys = wallet->listPublicKeys(); if (keys.count() == 0) { QJsonObject result{ { "status", "fail" }, { "message", "Uninitialized Wallet." } }; return emit buyResult(result); } QString key = keys[0]; // For now, we receive at the same key that pays for it. ledger->buy(key, cost, assetId, key, controlledFailure); }
int maxProfit(vector<int>& prices) { if(!prices.size()) return 0; int n = prices.size(); vector<int> buy(n,0); vector<int> sell(n,0); buy[0] = -prices[0]; for(int i = 1;i < n;++i){ if(i >= 2) buy[i] = max(buy[i-1],sell[i-2]-prices[i]); else buy[i] = max(buy[i-1],-prices[i]); sell[i] = max(sell[i-1],buy[i-1]+prices[i]); } return sell[n-1]; }
bool buy_with_check( int customer ) { // It is unsafe to use just is_free() followed by buy(customer), // since it violates atomicity. bool bought = false; pthread_mutex_lock( &mutex ); if ( is_free() ) { buy( customer ); bought = true; } pthread_mutex_unlock( &mutex ); return bought; }
// Very basic strategy: // - Sell if the price is >= 4 // - Buy if the price is <= 2 // - Lower price if combined opponents have more stock than I do // - Otherwise raise price by as little as possible void makeMove() { int die1, die2; scanf("%d %d", &die1, &die2); if (turnCounter == CRASH_AT_TURN) { fprintf(stderr, "Oh no, I crashed!\n"); exit(42); } else if (turnCounter == LOOP_AT_TURN) { while (true); } else if (turnCounter == BAD_MOVE_AT_TURN) { raise(MAX_DIE + 1 - die1, die2); printf("R %d %d\n", MAX_DIE + 1 - die1, die2); } else if (canSell(die1, die2)) { sell(me, die1, die2); printf("S %d %d\n", die1, die2); } else if (canSell(die2, die1)) { sell(me, die2, die1); printf("S %d %d\n", die2, die1); } else if (canBuy(die1, die2)) { buy(me, die1, die2); printf("B %d %d\n", die1, die2); } else if (canBuy(die2, die1)) { buy(me, die2, die1); printf("B %d %d\n", die2, die1); } else if (canLower(die1, die2)) { lower(die1, die2); printf("L %d %d\n", die1, die2); } else if (canLower(die2, die1)) { lower(die2, die1); printf("L %d %d\n", die2, die1); } else if (die1 < die2) { raise(die1, die2); printf("R %d %d\n", die1, die2); } else { raise(die2, die1); printf("R %d %d\n", die2, die1); } fflush(stdout); }
int maxProfit(vector<int>& prices) { int n = prices.size(); if (n < 2) return 0; vector<int> buy(n), sell(n); buy[0] = -prices[0]; buy[1] = max(buy[0], -prices[1]); sell[1] = max(0, -prices[0] + prices[1]); for (int i = 1; i < n; i++) { buy[i] = max(buy[i - 1], sell[i - 2] - prices[i]); sell[i] = max(sell[i - 1], buy[i - 1] + prices[i]); } return sell[n - 1]; }
int maxProfit(vector<int>& prices) { if (prices.size() <= 1) return 0; int n = prices.size(); vector<int> sell(n, 0); vector<int> buy(n, 0); buy[0] = -prices[0]; for (int i=1; i<prices.size(); i++) { if (i >= 2) buy[i] = buy[i-1]>sell[i-2]-prices[i]? buy[i-1] : sell[i-2]-prices[i]; else buy[i] = buy[i-1]>-prices[i]? buy[i-1] : -prices[i]; sell[i] = sell[i-1]>buy[i-1]+prices[i]? sell[i-1] : buy[i-1]+prices[i]; } return sell[n-1]; }
void buy(int indx, int target, int goods[], int sack[]) { if (!target) { for (int i = 0; i < 8; i++) if (sack[i]) printf("%d ", goods[i]); printf("\n"); return; } for (indx; indx < 8; indx++) { if (!sack[indx] && target - goods[indx] >= 0) { sack[indx] = 1; buy(indx + 1, target - goods[indx], goods, sack); sack[indx] = 0; } } return; }
int maxProfit(vector<int>& prices) { int n=prices.size(); if (n<2) return 0; vector<int> buy(n); buy[0]=-prices[0]; buy[1]=max(-prices[1], buy[0]); vector<int> sell(n); sell[0]=0; sell[1]=max(0, prices[1]-prices[0]); for (int i=2; i<n; ++i) { buy[i] = max(buy[i-1], sell[i-2]-prices[i]); sell[i] = max(sell[i-1], buy[i-1]+prices[i]); } return sell[n-1]; }
/*Function definitions, etc.*/ int main(){ /*START_OF_MAIN*/ char ch; char ch1; /*END_OF_VARIABLES*/ /*Yatırım yapılacak urunun secim menusu*/ printf("*****************INSTRUMENTS*****************\n"); printf(" TL: (L or l)\n"); printf(" USD: (D or d)\n"); printf(" Gold: (G or g)\n"); printf(" Investment Fund: (F or f)\n"); printf("********************************************\n"); printf("Istediginiz Yatirimin Kodunu Giriniz:"); scanf("%c",&ch1); /*Yapılacak operasyonların secim menusu*/ printf("\n*****************OPERATIONS*****************\n"); printf(" Transaction:(T/t)\n"); printf(" Buy:(B/b)\n"); printf(" Sell:(S/s)\n"); printf(" Input:(I/i)\n"); printf(" Output:(O/o)\n"); printf(" Report to File:(F/f)\n"); printf(" Report to Console:(C/c)\n"); printf(" Exit:(E/e)\n"); printf("********************************************\n"); printf("Istediginiz Islemin Islem Kodunu Giriniz:"); scanf("\n%c",&ch); /*Fonksiyonların cagırılması*/ intruments(ch1); operations(ch); trans(ch); buy(ch); sell(ch); inp(ch); outp(ch); rptfile(ch); rptcnsl(ch); exitt(ch); return 0; /*END_OF_MAIN*/ }
static int a_shop(int index, int subindex) { int i; // If index==99 then just build the menu. (No action). if (index == 99) { if (cheatlevel & CHEAT_MONEY) player[nowplayer].money = 1000000; wpos = 0; your_money(player[nowplayer].money); shop_filter(); shop_display(); backoff = 0; // No backoff allowed this time. return 0; } your_money(player[nowplayer].money); switch (index) { case 2: // Buy-sell given weapon. case 3: case 4: if (subindex == 0) buy(index-2); else sell(index-2); break; case 5: // Up down pressed. if ((subindex==0) && (wpos > 0)) wpos--; if ((subindex==1) && (wpos < nws-3)) wpos++; shop_filter(); shop_display(); break; case 6: // Save game buildmenu(savemenu); break; case 7: // Play next level. // Reflect changes made to weapon in PlayerState structure. psave.ships = player[nowplayer].ships; psave.money = player[nowplayer].money; for (i = 0; i < WEAPONSLOTS; i++) psave.slot[i] = player[nowplayer].slot[i]; return 1; } return 0; }
/** * shell function */ int shell() { debug("shell init"); checkLogin(); char input[MAXLENGTH]; for (;;) { printf("%s>", getUser()); read_line(input); if (sequals(input, "exit")) { debug("shut down"); shellExit(); } else if (begins(input, "set ")) { debug("exec set cmd"); parseSet(input + 4); } else if (begins(input, "list ")) { debug("exec list cmd"); parseList(input + 5); } else if (begins(input, "search ")) { debug("exec find cmd from search"); find(input + 7); } else if (begins(input, "find ")) { debug("exec find cmd"); find(input + 5); } else if (begins(input, "buy ")) { debug("exec buy cmd"); buy(input + 4); } else if (begins(input, "help")) { debug("exec help cmd"); printInternHelp(); } else if (begins(input, "balance")) { debug("exec balance cmd"); balanceUser(); } else { debug("no match found in shell"); printf("Don't know what you mean...\n"); } } return 0; }
int maxProfit_cooldown(vector<int> &prices){ //309 best tiem to buy and sell stock with cooldown if(prices.size() < 2) return 0; int i , n = prices.size() , res = 0; vector<int> buy(n , 0) , sell(n , 0); //buy[i]表示第i天买入后的最大收益 //seller[i]表示第i天卖出后的最大收益 buy[0] = -prices[0]; for(int i = 1 ; i < n ; i++){ sell[i] = max(buy[i - 1] + prices[i] , sell[i - 1] - prices[i - 1] + prices[i]); if(sell[i] > res) res = sell[i]; if(i == 1) buy[i] = buy[0] + prices[0] - prices[1]; else buy[i] = max(sell[i - 2] - prices[i] , buy[i - 1] + prices[i - 1] - prices[i]); } return res; }