http://pat.zju.edu.cn/contests/pat-a-practise/1060
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*105 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 10100,
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.d1...dN*10^k" (d1>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
下面的代码为嘛过不了呢=,=
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; struct ans{ string d; int k; }; ans convert(string s, int n){ ans tmp; if ((s[0] != '0') && (s.find(".") != string::npos)){ tmp.k = s.find("."); if (s.size() - 1 >= n){ if (tmp.k >= n) tmp.d = s.substr(0, n); else if (tmp.k < n){ tmp.d = s.substr(0, tmp.k) + s.substr(tmp.k+1, n-tmp.k); } } else{ tmp.d = s.substr(0, tmp.k) + s.substr(tmp.k + 1, s.size()-1-tmp.k); for (int i = 0; i < n - (s.size() - 1); i++){ tmp.d += "0"; } } } if ((s[0] != '0') && (s.find(".") == string::npos)){ tmp.k = s.size(); if (n <= tmp.k) tmp.d = s.substr(0, n); else if (n > tmp.k){ tmp.d = s; for (int i = 0; i < n - tmp.k; i++){ tmp.d += "0"; } } } if (s[0] = '0' && s != "0"){ int t; for (int i = 2; i < s.size(); i++){ if (s[i] != '0'){ t = i; tmp.k = -(i - 1); break; } } if (s.size() - t >= n) tmp.d = s.substr(t, n); else { tmp.d = s.substr(t, n); for (int i = 0; i < n - s.size() + t; i++){ tmp.d += "0"; } } } if (s == "0"){ tmp.k = 0; tmp.d = ""; for (int i = 0; i < n; i++){ tmp.d += "0"; } } return tmp; } int main(){ int n; string s1, s2; ans tmp1, tmp2; cin >> n >> s1 >> s2; tmp1 = convert(s1, n); tmp2 = convert(s2, n); if (tmp1.d == tmp2.d && tmp1.k == tmp2.k) cout << "YES" << " " << "0." << tmp1.d << "*10^" << tmp1.k << endl; else cout << "NO" << " " << "0." << tmp1.d << "*10^" << tmp1.k << " " << "0." << tmp2.d << "*10^" << tmp2.k << endl; return 0; }
我只能说坑太多,各种格式的数字也没描述清楚。。。真操蛋
AC代码:
#include <stdio.h> #include <string.h> #define MAX 110 struct result{ char d[MAX]; int 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) continue; else{ if (firstPos == -1) firstPos = i; if (index < n) { if (index < strlen(a)) r.d[index++] = a[i]; else r.d[index++] = '0'; } } } r.d[index] = 0; if (pointPos == -1) pointPos = i; if (pointPos - firstPos < 0) r.k = pointPos - firstPos + 1; else r.k = pointPos - firstPos; if (index == 0){ int 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); else printf("NO 0.%s*10^%d 0.%s*10^%d\n", r1.d, r1.k, r2.d, r2.k); return 0; }