十二硬币的C语言判别(共5页)_第1页
十二硬币的C语言判别(共5页)_第2页
十二硬币的C语言判别(共5页)_第3页
十二硬币的C语言判别(共5页)_第4页
十二硬币的C语言判别(共5页)_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上十二硬币判别代码 人间客制作国外流行的12个硬币问题:12硬币中可能有一个假的,而且不知道假币比真币重还是轻,给你一架天平,天平没有示数,只能判断是平衡还是不平,且只准称三次,让你判断是否有假币;如果有,找出来。解法思想如下:第一步:任选8个比较,如选: 比 一、若一样重,则假币在中,第二步: 比1.若一样重,则可能的假币为。则第三步: vs 1)若一样重,则没有假币;2)不一样重,则假币为:如果(1)>(12),则假币轻,反之,假币重; 2.若重,则第三步: 比 1)若一样重,则假币为(较轻)2)不一样重,则假币为、中较重者3.若轻,则第三步: 比 1)若一样

2、重,则假币为(较重)2)不一样重,则假币为、中较轻者二、若重,则第二步: 比 1.若一样重,则假币在中,第三步: 比 假币为、中较轻者2.若端较重,则假币在中,第三步:比 1)若一样重,则假币为(较轻)2)不一样重,则假币为中较重者3.若端较重,则假币在中,第三步: 比 1)若一样重,则假币为(较轻)2)不一样重,则假币为、中较重者三、若轻,则与上面类似,第二步:比 1.若一样重,则假币在中,第三步: 比 假币为、中较重者2.若端较轻,则假币在中,第三步: 比 1)若一样重,则假币为(较重)2)不一样重,则假币为中较轻者3.若端较轻,则假币在中,第三步: 比 1)若一样重,则假币为(较重)2)

3、不一样重,则假币为、中较轻者在科技发达的时代,我们都不愿意去做这样繁琐的比较,而是想借助于计算机的威力,下面给大家介绍一种方法,可以在很短的时间内完成中比较。当然,这类问题转化到编程上去后,主要考查的是大家的编程思想,而不再是12硬币问题本身了,因为12个硬币的重量需要大家自己去输入,这些数据只是为了检验大家程序的正确与否。下面是该问题的代码:#include <stdio.h>void main()int i;float a12;for(i=0;i<12;i+)scanf("%f",&ai);if(a0+a1+a2+a3=a4+a5+a6+a7)

4、 if(a0+a1+a2=a8+a9+a10) if(a8=a11) printf("There is no special coin!n"); else if(a8>a11) printf("There is a special coin:%f(12) and it's lighter than others.n",a11); else printf("There is a special coin:%f(12) and it's heavier than others.n",a11); else if(a0+

5、a1+a2>a8+a9+a10) if(a8=a9) printf("There is a special coin:%f(11) and it's lighter than others.n",a10); else if(a8>a9) printf("There is a special coin:%f(10) and it's lighter than others.n",a9); else printf("There is a special coin:%f(9) and it's lighter th

6、an others.n",a8); else if(a8=a9) printf("There is a special coin:%f(11) and it's heavier than others.n",a10); else if(a8>a9) printf("There is a special coin:%f(9) and it's heavier than others.n",a8); else printf("There is a special coin:%f(10) and it's he

7、avier than others.n",a9); else if(a0+a1+a2+a3>a4+a5+a6+a7) if(a0+a2+a5=a1+a4+a8) if(a6=a7) printf("There is a special coin:%f(4) and it's heavier than others.n",a3); else if(a6>a7) printf("There is a special coin:%f(8) and it's lighter than others.n",a7); else

8、printf("There is a special coin:%f(7) and it's lighter than others.n",a6); else if(a0+a2+a5>a1+a4+a8) if(a0=a2) printf("There is a special coin:%f(5) and it's lighter than others.n",a4); else if(a0>a2) printf("There is a special coin:%f(1) and it's heavier

9、than others.n",a0); else printf("There is a special coin:%f(3) and it's heavier than others.n",a2); else if(a1>a8) printf("There is a special coin:%f(2) and it's heavier than others.n",a1); if(a5<a8) printf("There is a special coin:%f(6) and it's light

10、er than others.n",a5); else if(a0+a2+a5=a1+a4+a8) if(a6=a7) printf("There is a special coin:%f(4) and it's lighter than others.n",a3); else if(a6>a7) printf("There is a special coin:%f(7) and it's heavier than others.n",a6); else printf("There is a special co

11、in:%f(8) and it's heavier than others.n",a7); else if(a0+a2+a5<a1+a4+a8) if(a0=a2) printf("There is a special coin:%f(5) and it's heavier than others.n",a4); else if(a0>a2) printf("There is a special coin:%f(3) and it's lighter than others.n",a2); else printf("There is a special coin:%f(1) and it's lighter than others.n",a0); else if(a1<a8) printf("There is a spe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论