日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1060 Are They Equal

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample 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越過了小數點,而小數點不能算作一位,注意的是這個次方是負值。

④比較相等時,如果基數部分一致、次方也一致,才算相等。

代碼如下:
?

#include<bits/stdc++.h> using namespace std;#define e exp(1) #define pi acos(-1) #define mod 1000000007 #define inf 0x3f3f3f3f #define ll long long #define ull unsigned long long #define mem(a,b) memset(a,b,sizeof(a)) int gcd(int a,int b){return b?gcd(b,a%b):a;}const int MAX=110; struct result{char d[MAX]; // 0.xxx部分int k; // 10的k次方 };result getResult(char *a, int n){result r;int firstPos = -1;int pointPos = -1;int index = 0;int i;for (i = 0; a[i]; i++){if (a[i] == '.'){pointPos = i;continue;}else if (a[i] == '0' && firstPos == -1) // 不能以0開頭,否則忽略continue;else{if (firstPos == -1)firstPos = i; // 第一個非0數字的位置if (index < n){if (index < strlen(a))r.d[index++] = a[i]; // 對于特定的精度,有數字則填入相應數字,沒有則補0elser.d[index++] = '0';}}}r.d[index] = 0; // 在數字結尾加\0,防止越界if (pointPos == -1)pointPos = i; // 如果沒有找到小數點,則小數點在最后,這是個純整數if (pointPos - firstPos < 0) // 判斷小數點與第一個非0數字的位置關系,計算10的幾次方r.k = - (firstPos - pointPos - 1); // 負次方,例如0.015,pointPos = 1, firstPos = 3, 3 - 1 - 1 = 1, -1是因為多算了小數點進去,0.15*10^-1elser.k = pointPos - firstPos; // 正次方,例如21.25,pointPos = 2,firstPos = 0,2-0=2,0.2125*10^2if (index == 0){ // 如果index = 0,代表值為0,則每一位都寫0,再加\0int i;for (i = 0; i != n; i++)r.d[i] = '0';r.d[i] = 0;r.k = 0;}return r; }int main(){int n;char a[MAX], b[MAX];scanf("%d%s%s", &n, a, b);result r1 = getResult(a, n);result r2 = getResult(b, n);if (strcmp(r1.d, r2.d) == 0 && r1.k == r2.k)printf("YES 0.%s*10^%d\n", r1.d, r1.k);elseprintf("NO 0.%s*10^%d 0.%s*10^%d\n", r1.d, r1.k, r2.d, r2.k);return 0; }

?

總結

以上是生活随笔為你收集整理的1060 Are They Equal的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。