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]; }
// 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 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; }
void bagfull(){ int dituID = 0; //地图 float x = 169; float y = 66; float z =0; if(IsBagFull()){ ReadProcessMemory(PID, (LPCVOID)(0x00903C7C), &dituID, 4, 0); ReadProcessMemory(PID, (LPCVOID)(dituID+0x1C), &dituID, 4, 0); ReadProcessMemory(PID, (LPCVOID)(dituID+0x8), &dituID, 4, 0); ReadProcessMemory(PID, (LPCVOID)(dituID+0x88), &dituID, 4, 0); if(dituID == 1){ x = 169; y = 66; }else if(dituID == 2){ x = 58; y = 112; }else if(dituID == 3){ x = -80; y = -145; } run(x,y,z); Sleep(100); CallActiveNPC(getGwIdByCString(sellNpcName)); sell(); Sleep(100); CallCloseWindow(); run(guajix,guajiy,guajiz); } }
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); } }
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); } } } }
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(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; }
void MainWindow::if_normal_card(int player_id,int card_id) { std::cout<<"\ninside normal card...\n"; if(array_of_cards[card_id].owner==0) { buy_button->setEnabled(true); pid=player_id; cid=card_id; connect(buy_button,SIGNAL(clicked()),this,SLOT(purchase())); std::cout<<"\nbuy_button connected...\n"; } else { if((player_id+1)==array_of_cards[card_id].owner) { sell_button->setEnabled(true); build_button->setEnabled(true); pid=player_id; cid=card_id; connect(sell_button,SIGNAL(clicked()),this,SLOT(sell())); connect(build_button,SIGNAL(clicked()),this,SLOT(renovate())); } else { sell_button->setEnabled(false); build_button->setEnabled(false); buy_button->setEnabled(false); pay_rent(player_id,card_id); } } }
/*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; }
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; }
// 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; } }
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]; }
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()]; }
// 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) { 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]; }
/* * 函数 void parse_command(char *command); * 参数:game 指向游戏数据结构体的指针 * 参数:command 当前玩家输入的操作命令字符串 * 作用:解析当前玩家输入的操作命令,执行相应的命令 * 作者:张泰然 */ void parse_command(Game *game, char *command) { char *pchar; // 用于存储字符串中某命令字符串首次出现的地址 int number; // 用于存储部分命令的参数 if (strstr(command, "query") != NULL) // 输入命令为query { query(game, &game->players[game->current_player_index]); } else if (strstr(command, "help") != NULL) // 输入命令为help { help(); } else if (strstr(command, "sell") != NULL) // 输入命令为sell n { pchar = strstr(command, "sell"); pchar += 4; while (isspace(*pchar)) { pchar++; // 跳过空白字符 } if (*pchar != '\0') { number = atoi(pchar); sell(game, number); } } else if (strstr(command, "step") != NULL) // 输入命令为step n { pchar = strstr(command, "step"); pchar += 4; while (isspace(*pchar)) { pchar++; // 跳过空白字符 } if (*pchar != '\0') { number = atoi(pchar); step(game, number); } } else if (strstr(command, "quit") != NULL) // 输入命令为quit { quit(); } else // 输入的不是合法命令 { printf("您输入的不是合法命令,请输入help命令查看命令帮助\n"); } system("pause"); // 暂停以避免闪烁 }
int maxProfit(vector<int>& prices) { if(prices.size() <= 1) return 0; int n = prices.size(); vector<int> sell(n, 0); vector<int> cooldown(n, 0); sell[1] = max(0, prices[1]-prices[0]); for(int i = 2;i < n;i++) { cooldown[i] = max(cooldown[i-1], sell[i-1]); sell[i] = max(cooldown[i-1], sell[i-1]+prices[i]-prices[i-1]); } return max(sell[n-1], cooldown[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]; }
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 main() { struct goods *head=NULL; int x,y,z,a,b,c; do { printf("欢迎使用商品库存信息管理系统!\n1. 输入商品信息2. 销售3. 进货4. 列举商品信息5. 清除所有商品6. 退出\n请输入您的选择:"); scanf("%d",&x); if(x==1) { head=create(); printf("\n"); } if(x==2) { printf("请输入商品号和销售数量(以逗号分隔):"); scanf("%d,%d",&y,&z); head=sell(y,z); printf("\n"); } if(x==3) { printf("请输入商品号和进货数量(以逗号分隔):"); scanf("%d,%d",&a,&b); head=stock(a,b); printf("\n"); } if(x==4) { list(head); printf("\n"); } if(x==5) { printf("确认清除输入1,否则输入0:"); scanf("%d",&c); if(c==1) { destroy(head); head=NULL; } printf("\n"); } }while(x!=6); printf("\n"); return 0; }
/*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; }
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]; }
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; }
void Trigger::evalData(L1Data * data) { if(symbol_ == data->symbol_) { l1Data_.push(data); if(buy(data)) { sendTrigger(PurchaseData::Buy, data); } else if(sell(data)) { sendTrigger(PurchaseData::Sell, data); } if(l1Data_.size() > 10) { L1Data * front = l1Data_.front(); l1Data_.pop(); delete front; } } }
void main()/* This is Main function. This function will executed on program startup */ { short int blnRun=true; do{ switch(menu_main()) { case 1: sell(); break; case 2: list(); break; case 3: create(); break; case 4: edit(); break; case 5: del(); break; case 6: sort(); break; case 7: search(); break; case 8: fSave(); break; case 9: fLoad(); break; case 0: default: blnRun=false; } }while(blnRun==true); clrscr(); puts("Press Enter to exit application..."); getch(); }
void Algo_turtle_intraday::on_data(const std::vector<time_point>& times, const std::vector<price_type> prices) { auto t = times.back(); auto p = prices.back(); if (( t - times.front() ) > _warm_up_period ) { _is_warming_up = false; } else { return; } auto first_time_at_long_period = std::find_if(times.begin(), times.end(), [t, this](time_point ti){return ti >= (t - this->_long_period); }); auto first_price_at_long_period = prices.begin() + (first_time_at_long_period - times.begin()); auto p_high = std::max_element(first_price_at_long_period, prices.end() - 1); if (p > (*p_high)* Algo_turtle_intraday::tolerance_high ) { if ( _portfolio_manager->can_buy() ) { buy(t, Algo_turtle_intraday::sym, p, 1); } } auto first_time_at_short_period = std::find_if(times.begin(), times.end(), [t, this](time_point ti){return ti >= (t - this->_short_period); }); auto first_price_at_short_period = prices.begin() + (first_time_at_short_period - times.begin()); auto p_low = std::min_element(first_price_at_short_period, prices.end() - 1); if (p < (*p_low) * Algo_turtle_intraday::tolerance_low) { if ( _portfolio_manager->can_sell(Algo_turtle_intraday::sym) ) { sell(t, Algo_turtle_intraday::sym, p, 1); } } }
// Function to process which action to take with current trade void portfolio::action(pTrade input){ // cout << "Enter action" << endl; if(input.sortID == "CD") return; if(input.type == "buy" || input. type == "Buy") buy(input); else if (input.type == "sell" || input.type == "Sell") sell(input); else if (input.type == "donate" || input.type == "Donate") donate(input); else if (input.type == "aquisition" || input.type == "Aquisition") acquistion(input); else if(input.type == "spinoff" || input.type == "Spinoff") spinoff(input); else if(input.type == "reverse split" || input.type == "split") split(input); else if(input.type == "exchange") exchange(input); else if(input.type == "dividend") dividend(input); // cout << "Exit action" << endl << endl; }
// method 1 // buy[i] means profit of buy on day i, not max profit // sell[i] means profit of sell on day, not max profit int maxProfit(vector<int>& prices) { if(prices.size()<2) return 0; vector<int> buy(prices.size()); vector<int> sell(prices.size()); buy[0]=-prices[0]; int mProfit=0; for(int i=1;i<prices.size();i++) { if(i==1) buy[i]= - prices[1]; else // two situation // sell[i-2]-prices[i] means sold on day i-2 and buy on day i after cooldown // buy[i-1]-prices[i]+prices[i-1] means if day i-1 can buy, then day i can buy too. calculate the difference buy[i]=max(buy[i-1]-prices[i]+prices[i-1],sell[i-2]-prices[i]); // two situation // buy[i-1]+prices[i] means buy on i-1 and sell on i // sell[i-1]-prices[i-1]+prices[i] means if day i-1 can sell, then day i can sell too. calculate the difference sell[i]=max(buy[i-1]+prices[i],sell[i-1]-prices[i-1]+prices[i]); mProfit=max(mProfit,sell[i]); } return mProfit; }