stock.c

mail@pastecode.io avatar
unknown
plain_text
6 months ago
931 B
3
Indexable
Never
#include <stdio.h>

int main()
{
    int money; 
    scanf("%d", &money);
    int originalmoney = money;
    int a[12]; 

    for (int i = 0; i < 12; i++) 
        scanf("%d", &a[i]);

    int stock = 0; 
    int buyprice = 0; 

    for (int i = 0; i < 11; i++) 
    {
        if (a[i] <= money) 
        {
            int buy = money / a[i]; //計算可買入股數
            money -= buy * a[i]; //計算買股票後剩餘金額
            stock += buy; //加上購入股數
            buyprice = a[i]; //購入股票價格
        }
        else if (a[i] > buyprice && a[i] > a[i + 1]) 
        {
            money += stock * a[i]; 
            stock = 0; 
            buyprice = 0; 
        }
    }

    if (stock > 0) //若迴圈結束仍有持股,以最後一個月價格出售
        money += stock * a[11]; 
    

    printf("%d\n", money - originalmoney); 

    return 0; 
}
Leave a Comment