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解析:
然后設計一個函數,對于輸入的數字(存儲在char*內)數組進行遍歷。
遍歷時定義firstPos記錄第一個非0數,pointPos記錄小數點位置。
需要注意以下問題:
①題目的case似乎出現了00123.45這種坑爹的情況,因此要處理無效的0,也就說開頭出現的0是不能要的,只有碰到第一個非0才記錄firstPos。
②對于題目給定的精度,如果輸入的數字位數不夠,要補0,在結尾處還要補\0使得字符串可靠結束。
③處理完畢后,對于firstPos和pointPos做比較,如果前者小,說明是大于1的數,pointPos - firstPos即為10的幾次方;如果后者小,說明是小數,應該用firstPos - pointPos + 1,+1是因為firstPos越過了小數點,而小數點不能算作一位,注意的是這個次方是負值。
④比較相等時,如果基數部分一致、次方也一致,才算相等。
代碼如下:
?
?
總結
以上是生活随笔為你收集整理的1060 Are They Equal的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最优化学习笔记(一)——牛顿法(一维搜索
- 下一篇: 1063 Set Similarity