stock.c

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

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

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

    long long int stock = 0; 

    for (int i = 0; i < 11; i++) 
    {
        if (a[i] <= a[i + 1]) //若當月價格小於等於下個月則買進
        {
            long long int buy = money / a[i]; //計算可買入股數
            money -= buy * a[i]; //計算買股票後剩餘金額
            stock += buy; //加上購入股數
        }
        else if (a[i] > a[i + 1]) //若當月價格大於等於下個月則賣出
        {
            money += stock * a[i]; 
            stock = 0; 
        }
    }

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

    return 0; 
}
Leave a Comment