12个硬币称三次c语言,十二硬币的C语言判别

本文介绍了如何使用编程解决一个经典的逻辑谜题——十二硬币问题。在仅允许使用天平称三次的情况下,找出一个可能较重或较轻的假硬币。文章提供了C++代码实现,通过比较不同组合的硬币重量来确定假硬币的位置和重量状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

十二硬币判别代码

人间客制作

国外流行的12个硬币问题:12硬币中可能有一个假的,而且不知道假币比真币重还是轻,给你一架天平,天平没有示数,只能判断是平衡还是不平,且只准称三次,让你判断是否有假币;如果有,找出来。解法思想如下:

第一步:任选8个比较,如选:⑴⑵⑶⑷ 比 ⑸⑹⑺⑻

一、若一样重,则假币在⑼~⑿中,第二步:⑼⑽ 比⑾⑴

1.若一样重,则可能的假币为⑿。则第三步:⑴ vs ⑿

1)若一样重,则没有假币;

2)不一样重,则假币为⑿:如果(1)>(12),则假币轻,反之,假币重;

2.若⑼⑽重,则第三步:⑼ 比 ⑽

1)若一样重,则假币为⑾(较轻)

2)不一样重,则假币为⑼、⑽中较重者

3.若⑼⑽轻,则第三步:⑼ 比 ⑽

1)若一样重,则假币为⑾(较重)

2)不一样重,则假币为⑼、⑽中较轻者

二、若⑴⑵⑶⑷重,则第二步:⑴⑵⑸ 比 ⑶⑷⑹

1.若一样重,则假币在⑺⑻中,第三步:⑺ 比 ⑻

假币为⑺、⑻中较轻者

2.若⑴⑵⑸端较重,则假币在⑴⑵⑹中,第三步:⑴比 ⑵

1)若一样重,则假币为⑹(较轻)

2)不一样重,则假币为⑴⑵中较重者

3.若⑶⑷⑹端较重,则假币在⑶⑷⑸中,第三步:⑶ 比 ⑷

1)若一样重,则假币为⑸(较轻)

2)不一样重,则假币为⑶、⑷中较重者

三、若⑴⑵⑶⑷轻,则与上面类似,第二步:⑴⑵⑸比 ⑶⑷⑹

1.若一样重,则假币在⑺⑻中,第三步:⑺ 比 ⑻

假币为⑺、⑻中较重者

2.若⑴⑵⑸端较轻,则假币在⑴⑵⑹中,第三步:⑴ 比 ⑵

1)若一样重,则假币为⑹(较重)

2)不一样重,则假币为⑴⑵中较轻者

3.若⑶⑷⑹端较轻,则假币在⑶⑷⑸中,第三步:⑶ 比 ⑷

1)若一样重,则假币为⑸(较重)

2)不一样重,则假币为⑶、⑷中较轻者

在科技发达的时代,我们都不愿意去做这样繁琐的比较,而是想借助于计算机的威力,下面给大家介绍一种方法,可以在很短的时间内完成中比较。当然,这类问题转化到编程上去后,主要考查的是大家的编程思想,而不再是12硬币问题本身了,因为12个硬币的重量需要大家自己去输入,这些数据只是为了检验大家程序的正确与否。下面是该问题的代码:

#include void main()

{

int i;

float a[12];

for(i=0;i<12;i++)

scanf("%f",&a[i]);

if(a[0]+a[1]+a[2]+a[3]==a[4]+a[5]+a[6]+a[7])

{

if(a[0]+a[1]+a[2]==a[8]+a[9]+a[10])

{

if(a[8]==a[11])

printf("There is no special coin!\n");

else if(a[8]>a[11])

printf("There is a special coin:%f(12) and its lighter than others.\n",a[11]);

else

printf("There is a special coin:%f(12) and its heavier than others.\n",a[11]);

}

else if(a[0]+a[1]+a[2]>a[8]+a[9]+a[10])

{

if(a[8]==a[9])

printf("There is a special coin:%f(11) and its lighter than others.\n",a[10]);

else if(a[8]>a[9])

printf("There is a special coin:%f(10) and its lighter than others.\n",a[9]);

else

printf("There is a special coin:%f(9) and its lighter than others.\n",a[8]);

}

else

{

if(a[8]==a[9])

printf("There is a special coin:%f(11) and its heavier than others.\n",a[10]);

else if(a[8]>a[9])

printf("There is a special coin:%f(9) and its heavier than others.\n",a[8]);

else

printf("There is a special coin:%f(10) and its heavier than others.\n",a[9]);

}

}

else if(a[0]+a[1]+a[2]+a[3]>a[4]+a[5]+a[6]+a[7])

{

if(a[0]+a[2]+a[5]==a[1]+a[4]+a[8])

{

if(a[6]==a[7])

printf("There is a special coin:%f(4) and its heavier than others.\n",a[3]);

else if(a[6]>a[7])

printf("There is a special coin:%f(8) and its lighter than others.\n",a[7]);

else

printf("There is a special coin:%f(7) and its lighter than others.\n",a[6]);

}

else if(a[0]+a[2]+a[5]>a[1]+a[4]+a[8])

{

if(a[0]==a[2])

printf("There is a special coin:%f(5) and its lighter than others.\n",a[4]);

else if(a[0]>a[2])

printf("There is a special coin:%f(1) and its heavier than others.\n",a[0]);

else

printf("There is a special coin:%f(3) and its heavier than others.\n",a[2]);

}

else

{

if(a[1]>a[8])

printf("There is a special coin:%f(2) and its heavier than others.\n",a[1]);

if(a[5]a[7])

printf("There is a special coin:%f(7) and its heavier than others.\n",a[6]);

else

printf("There is a special coin:%f(8) and its heavier than others.\n",a[7]);

}

else if(a[0]+a[2]+a[5]a[2])

printf("There is a special coin:%f(3) and its lighter than others.\n",a[2]);

else

printf("There is a special coin:%f(1) and its lighter than others.\n",a[0]);

}

else

{

if(a[1]a[8])

printf("There is a special coin:%f(6) and its heavier than others.\n",a[5]);

}

}

}

部分结果的截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值