#54768: C語言


rumm2859@gmail.com (青)


#include<stdio.h>

int main(){

    宣告a為Wayne帶的錢、b為每隔幾分鐘吃一次、c為開始吃的食物
    int a,b,c;

    讀取總金額、進食間隔時間、開始食物
    scanf("%d %d %d",&a,&b,&c);

    如果一開始就買不起要吃的食物
    if((c==0 && a<32) || (c==1 && a<55)){

        輸出Wayne無法吃東西的訊息
        printf("Wayne can't eat and drink.");

        結束程式
        return 0;
    }

    使用for迴圈模擬Wayne吃東西的過程,d代表經過的時間
    for(int d=0; (c==0 && a>=32) || (c==1 && a>=55); d+=b){

        如果c等於0代表吃蘋果派
        if(c==0){

            輸出目前時間與吃蘋果派的訊息
            printf("%d: Wayne eats an Apple pie, and now ",d);

            扣掉蘋果派價格32元
            a-=32;
        }

        否則代表喝玉米濃湯
        else{

            輸出目前時間與喝玉米濃湯的訊息
            printf("%d: Wayne drinks a Corn soup, and now ",d);

            扣掉玉米濃湯價格55元
            a-=55;
        }

        如果剩餘金額為0
        if(a==0)

            輸出沒有錢的訊息
            printf("he doesn't have money.\n");

        如果剩餘金額為1
        else if(a==1)

            輸出單數dollar
            printf("he has 1 dollar.\n");

        否則輸出剩餘金額(dollars)
        else
            printf("he has %d dollars.\n",a);

        將c在0與1之間切換,使食物交替
        c = 1 - c;
    }

    程式正常結束
    return 0;
}