1060 Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as?0.123×10?5???with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.
Input Specification:
Each input file contains one test case which gives three numbers?N,?A?and?B, where?N?(<100) is the number of significant digits, and?A?and?B?are the two float numbers to be compared. Each float number is non-negative, no greater than?10?100??, and that its total digit number is less than 100.
Output Specification:
For each test case, print in a line?YES?if the two numbers are treated equal, and then the number in the standard form?0.d[1]...d[N]*10^k?(d[1]>0 unless the number is 0); or?NO?if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.
Sample Input 1:
3 12300 12358.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3解析:
然后設(shè)計(jì)一個(gè)函數(shù),對(duì)于輸入的數(shù)字(存儲(chǔ)在char*內(nèi))數(shù)組進(jìn)行遍歷。
遍歷時(shí)定義firstPos記錄第一個(gè)非0數(shù),pointPos記錄小數(shù)點(diǎn)位置。
需要注意以下問題:
①題目的case似乎出現(xiàn)了00123.45這種坑爹的情況,因此要處理無效的0,也就說開頭出現(xiàn)的0是不能要的,只有碰到第一個(gè)非0才記錄firstPos。
②對(duì)于題目給定的精度,如果輸入的數(shù)字位數(shù)不夠,要補(bǔ)0,在結(jié)尾處還要補(bǔ)\0使得字符串可靠結(jié)束。
③處理完畢后,對(duì)于firstPos和pointPos做比較,如果前者小,說明是大于1的數(shù),pointPos - firstPos即為10的幾次方;如果后者小,說明是小數(shù),應(yīng)該用firstPos - pointPos + 1,+1是因?yàn)閒irstPos越過了小數(shù)點(diǎn),而小數(shù)點(diǎn)不能算作一位,注意的是這個(gè)次方是負(fù)值。
④比較相等時(shí),如果基數(shù)部分一致、次方也一致,才算相等。
代碼如下:
?
?
總結(jié)
以上是生活随笔為你收集整理的1060 Are They Equal的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最优化学习笔记(一)——牛顿法(一维搜索
- 下一篇: 1063 Set Similarity