用c语言写啤酒和饮料

Python017

用c语言写啤酒和饮料,第1张

/*啤酒2块1瓶4盖换1瓶2空瓶换1瓶10块喝几瓶? .用C语言编程解决*/

#include

#define PRICE 2//单价

#define COV 4//每4盖换瓶

#define ENP 2//每2瓶换瓶

int main(int argc, char* argv[])

{

int enpty = 0//空瓶

int cover = 0//盖

int money = 10//钱

int beer = 0// 啤酒

while(enpty>0||cover>0||money>0)

{

for(money>0money-=PRICE)

{

enpty++

cover++

beer++

}

for(enpty>0enpty-=ENP)

{

enpty++

cover++

beer++

}

for(cover>0cover-=COV)

{

enpty++

cover++

beer++

}

}

printf("10块洋喝%d瓶啤酒、\n", beer)

return 0

}

#include<stdio.h>

int main()

{

double m, n, k, l

int num_pi, num_yin, num_yin_max, flag = 0

scanf("%lf %lf %lf", &m, &n, &k)

num_yin_max = (int) (k/n)

for(num_yin=1num_yin <= num_yin_maxnum_yin++)

{

for(num_pi=1num_pi<num_yinnum_pi++)

{

l = k - (num_pi*m + num_yin*n)

if((l<0.000001) &&(l>-0.000001))

{

printf("%d %d", num_pi, num_yin)

flag = 1

break

}

}

if(flag == 1)

{

break

}

}

if(flag == 0)

printf("0")

return 0

}

代码截图

运行结果

有一个问题就是默认啤酒和饮料不为0,若可以为0的话,改一下啤酒的循环就可以了

#include<stdio.h>

#include<conio.h>

main()

{

int i=1

char getchviewk

for( i =1i<=5i++)

{

printf("[1]可乐 [2]咖啡\n")

printf("[3]果汁 [4]奶茶\n")

printf("[0]退出\n")

if(i==1)getchviewk=getch()

switch(getchviewk)

{

case 48 : i=5break

case 49 :printf("可乐3元\n")getchviewk=getch()break

case 50 :printf("咖啡5元\n")getchviewk=getch()break

case 51 :printf("果汁4元\n")getchviewk=getch()break

case 52 :printf("奶茶2元\n")getchviewk=getch()break

default : printf("显示错误提示信息\n")getchviewk=getch()

}

system("cls")

}

return 0

}