m0_68809217 2022-04-24 22:03
浏览 28
已结题

一道C语言题,自己打的代码在循环处有问题。

题目:波瓦松的分酒趣题。法国著名数学家波瓦松青年时代研究过一个有趣的数学问题:某人有12品脱的啤酒一瓶,想从中倒出6品脱,但他没有6品脱的容器,仅有一个8品脱和一个5品脱的容器,怎样倒才能将啤酒分为两个6品脱呢?
要求格式:
Input Full a,Empty b,c,Get i: 12 8 5 6
a12 b8 c5
12 0 0
4 8 0
4 3 5
9 3 0
9 0 3
1 8 3
1 6 5
6 6 0
a12 b5 c8
12 0 0
7 5 0
7 0 5
2 5 5
2 2 8
10 2 0
10 0 2
5 5 2
5 0 7
0 5 7
0 4 8
8 4 0
8 0 4
3 5 4
3 1 8
11 1 0
11 0 1
6 5 1
6 0 6

我的代码:(amax,bmax,cmax代表三瓶的最大容量。a,b,c代表三瓶中的酒量)

#include<stdio.h>
void Give(int* a,int* b,int* c,int which,int amax,int bmax,int cmax)
{
    if(which==*a)//c->a
    {
        *a+=*c;
        *c=0;
        printf("%4d%4d%4d\n",*a,*b,*c);
    }

    else if(which==*b)//a->b
    {
        *b=*a;
        if(*b>bmax)
        {
            *b=bmax;
            *a-=bmax;
        }
        else
        {
            *a=0;
        }
        printf("%4d%4d%4d\n",*a,*b,*c);
    }

    else if(which==*c)//b->c
    {
        *c=*b;
        if(*c>cmax)
        {
            *c=cmax;
            *b-=cmax;
        }
        else
        {
            *b=0;
        }
        printf("%4d%4d%4d\n",*a,*b,*c);
    }

    else
    {
        printf("progrom wrong");
    }
}

int main()
{
    printf("Input Full a,Empty b,c,Get i:\t");//i为最后结果
    int a,b,c,i=6;
    int amax,bmax,cmax;

    a=12,b=0,c=0;
    amax=12,bmax=8,cmax=5;
    printf("  a%d  b%d  c%d\n%4d%4d%4d\n",amax,bmax,cmax,a,b,c);
    do{
        Give(&a,&b,&c,b,amax,bmax,cmax);//a->b
        if(a==i && b==i)
            break;
        Give(&a,&b,&c,c,amax,bmax,cmax);//b->c
        if(a==i && b==i)
            break;
        Give(&a,&b,&c,a,amax,bmax,cmax);//c->a
        if(a==i && b==i)
            break;
        Give(&a,&b,&c,c,amax,bmax,cmax);//b->c
    }while(a!=i && b!=i);

    a=12,b=0,c=0;
    amax=12,bmax=5,cmax=8;
    printf("  a%d  b%d  c%d\n%4d%4d%4d\n",amax,bmax,cmax,a,b,c);
    do{
        Give(&a,&b,&c,b,amax,bmax,cmax);//a->b
        if(a==i && b==i)
            break;
        Give(&a,&b,&c,c,amax,bmax,cmax);//b->c
        if(a==i && b==i)
            break;
        Give(&a,&b,&c,a,amax,bmax,cmax);//c->a
        if(a==i && b==i)
            break;
        Give(&a,&b,&c,c,amax,bmax,cmax);//b->c
    }while(a!=i && b!=i);
    return 0;
}

debug的结果:打完断点后,一按debug就三行0

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 5月2日
    • 创建了问题 4月24日

    悬赏问题

    • ¥15 CATIA有些零件打开直接单机确定终止
    • ¥15 请问有会的吗,用MATLAB做
    • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
    • ¥15 ARIMA模型时间序列预测用pathon解决
    • ¥15 孟德尔随机化怎样画共定位分析图
    • ¥18 模拟电路问题解答有偿速度
    • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
    • ¥15 误删注册表文件致win10无法开启
    • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
    • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址