The topics are as follows:
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 is considered equal since they is B Oth saved as 0.123*105 with simple chopping. Now given the number of significant digits in a machine and both float numbers, you is supposed to tell if they is treate D equal in this 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 Significan t digits, and A and B are the numbers of the float to be compared. Each of the float number is non-negative, no greater than, and that it total digit number are less than.
Output Specification:
for each test case, print in a line" YES "If the numbers is treated equal, and the n the number in the standard form "0.d1...dn*10^k" (D1>0 unless the number is 0); or "NO" if they is not a treated equal, and then the "numbers in their" form. All the terms must is 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
The difficulty with this topic is that the number of treatments is not necessarily greater than 1, There may also be 0.015 such numbers, my previous code can only handle more than 1 of the case, and then refer to the small 5555 of the code, found that two cursors can indicate the decimal point and the first non-0 number of positions, and then determine the location of the relationship between the number of values.
First define a struct to store the cardinality and the sub-side:
struct Result{char D[max];//0.xxx part int k;//10 k-th square};
A function is then designed to iterate over an array of input numbers (stored in char*).
Diachronic definition Firstpos Record the first non-0 number, Pointpos record the decimal position.
The following issues need to be noted:
① the case of the topic seems to appear 00123.45 this kind of pit father situation, so to deal with the invalid 0, also said that the beginning of 0 is not to be, only hit the first non-0 to record Firstpos.
② for the given accuracy of the topic, if the number of digits entered is not enough, to fill 0, at the end of the addition of the string to make a reliable end.
③ after processing, for Firstpos and Pointpos to do a comparison, if the former small, the description is greater than 1 of the number, Pointpos-firstpos is 10 of several Parties; if the latter is small, the description is a decimal, should be used Firstpos-pointpos + 1,+ 1 is because the Firstpos crosses the decimal point, and the decimal point is not counted as one, note that this is a negative value.
When the ④ is equal, it is equal if the cardinality part is consistent and the sub-party is identical.
The code is as follows:
#include <stdio.h> #include <string.h> #define MAX 110struct Result{char D[max];//0.xxx part int k;//10 k-th square};re Sult 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)//cannot start with 0, otherwise ignore continue;else{if (Firstpos = =-1) firstpos = i;//position of first non-0 digit if (Index < n) {if (Index < strlen (a)) r.d[index++] = a[i];//For a specific precision, a number is filled with the corresponding number, no then fill 0elser.d[index++] = ' 0 ';}}} R.d[index] = 0; Add to the end of the number to prevent cross-border if (Pointpos = =-1) pointpos = i; If the decimal point is not found, then the decimal point is at the end, which is a pure integer if (Pointpos-firstpos < 0)//judge the position of the decimal point with the first non-0 digit, calculate 10 R.K =-(firstpos-pointpos-1) ; Negative sub-parties, such as 0.015,pointpos = 1, Firstpos = 3, 3-1-1 = 1, 1 is because more than the decimal point in, 0.15*10^-1ELSER.K = Pointpos-firstpos; The positive side, for example 21.25,pointpos = 2,firstpos = 0,2-0=2,0.2125*10^2if (index = = 0) {//If index = 0, which represents a value of 0, then each bit is written 0, plus \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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1060. is they Equal (25)