1060. Are They Equal (25)

来源:互联网 发布:单片机设计实例 编辑:程序博客网 时间:2024/06/09 23:04

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 <stdio.h>      #include <string.h>      #define 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]; // 对于特定的精度,有数字则填入相应数字,没有则补0                      else                          r.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^-1          else              r.k = pointPos - firstPos; // 正次方,例如21.25,pointPos = 2,firstPos = 02-0=20.2125*10^2          if (index == 0){ // 如果index = 0,代表值为0,则每一位都写0,再加\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;      } 
原创粉丝点击