




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上十二硬幣判別代碼 人間客制作國外流行的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、不一樣重,則假幣為、中較輕者在科技發(fā)達的時代,我們都不愿意去做這樣繁瑣的比較,而是想借助于計算機的威力,下面給大家介紹一種方法,可以在很短的時間內完成中比較。當然,這類問題轉化到編程上去后,主要考查的是大家的編程思想,而不再是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. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 壞死性腦炎護理要點與實施路徑
- 基礎教育課程代理銷售框架協議
- 植物新品種培育與農業(yè)品牌建設協議
- 網絡劇音樂版權授權及廣告合作合同
- 網上商店店鋪轉讓及后期服務保障合同
- 網紅景點特許經營合作協議書
- 紡織品質量追溯體系解決方案定制合同
- 2025年電位差計項目建議書
- 婚姻忠誠監(jiān)管實施細節(jié)及違規(guī)處理協議
- 跨國公司股權轉讓與市場準入許可協議
- aeo供應鏈安全培訓
- 《審計實務》第6講 函證程序(下)
- 物流運輸企業(yè)安全隱患排查記錄表
- 神經病學題庫及神經病學試題題庫
- 糖尿病酮癥完整版本
- 五年級下冊數學約分練習100題附答案
- 跨文化交流中的語言適應性研究
- 第5.2課 《飛向太空的航程》同步練習 (原卷版)
- 新概念英語第二冊Lesson8課件
- 地攤市集活動策劃方案
- 勞務外包與勞務派遣課件
評論
0/150
提交評論