题目连接:https://www.patest.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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3 失落……考察科学计数法,关键在于小数点和第一个有效位的位置的确定。参考了他人的代码http://blog.csdn.net/xtzmm1215/article/details/38809629
1 #include<stdio.h>
2 #define MAXN 105
3 #include<string.h>
4 #include<stdlib.h>
5 struct result{
6 char d[MAXN];
7 int k;
8 };
9
10 result getResult(char *a,int n)
11 {
12 int firstPos=-1,pointPos=-1;
13 int index=0;
14 result r;
15 int i=0;
16 for ( i=0;a[i];i++)
17 {
18 if (a[i]=='.')
19 {
20 pointPos=i;
21 continue;
22 }
23 if (firstPos==-1 && a[i]=='0')continue;
24 else
25 {
26 if (firstPos==-1 && a[i]!='0')
27 {
28 firstPos=i;
29 }
30 if (index<n)
31 {
32 r.d[index++]=a[i];
33 }
34 }
35 }
36 r.d[index]=0;
37
38 if (pointPos==-1)pointPos=i;
39 if (pointPos-firstPos<0)r.k=pointPos-firstPos+1;
40 else r.k=pointPos-firstPos;
41
42 int j;
43 if (index==0)
44 {
45 for (j=0;j<n;j++)r.d[j]='0';
46 r.d[j]=0;
47 r.k=0;
48 }
49 return r;
50 }
51
52 int main()
53 {
54 int N;
55 scanf("%d",&N);
56 char s1[MAXN],s2[MAXN];
57 result r1,r2;
58 scanf("%s %s",s1,s2);
59 r1=getResult(s1,N);
60 r2=getResult(s2,N);
61 if (strcmp(r1.d,r2.d)==0 && r1.k==r2.k)
62 {
63 printf("YES 0.%s*10^%d",r1.d,r1.k);
64 }
65 else printf("NO 0.%s*10^%d 0.%s*10^%d",r1.d,r1.k,r2.d,r2.k);
66 return 0;
67 }
来源:https://www.cnblogs.com/wuxiaotianC/p/6439399.html