1060. Are They Equal (25)

来源:互联网 发布:线性时间选择算法分析 编辑:程序博客网 时间:2024/05/17 17:14

1060. Are They Equal (25)

 

时间限制
50 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<stdlib.h>#include<string.h>#define MAX 110int getK(char *a){    int DotPos,FirstValid;    int i;    DotPos = -1;FirstValid = -1;    for(i=0;a[i];i++)    {        if(a[i] == '.')        {            DotPos = i;        }else if(a[i] != '0' && FirstValid == -1){FirstValid = i;}    }if(DotPos == -1){DotPos = i;}if(FirstValid == -1){return 0;}if(DotPos < FirstValid){   return DotPos - FirstValid+1;}else{return DotPos - FirstValid;}}char *convert(char *a,int N){    char *c;    int lenth,i,flag,count;    c = (char *)malloc(MAX*sizeof(char));    c[0] = '0';    c[1] = '.';    lenth = strlen(a);    flag = 1;    count = 0;    for(i=0;i<lenth;i++)    {        if(a[i] != '.' && a[i] !='0')        {            flag = 0;        }if(a[i] == '.'){continue;}        if(flag)        {            continue;        }        c[count+2] = a[i];        count ++;        if(count >= N)        {            break;        }    }    while(count < N)    {        c[count+2] = '0';        count++;    }    c[count+2] = 0;    return c;}int main(){    int N;    char a[MAX],b[MAX];    char *c,*d;    //freopen("d://input.txt","r",stdin);    scanf("%d%s%s",&N,a,b);    c = convert(a,N);    d = convert(b,N);if(getK(a) == getK(b) && strcmp(c,d)==0){ printf("YES %s*10^%d\n",c,getK(a),d,getK(b));}else{printf("NO %s*10^%d %s*10^%d\n",c,getK(a),d,getK(b));}    return 0;}



 

 

 

0 0
原创粉丝点击