Title Description
DescriptionGiven two floating-point numbers, please determine if the two floating-point numbers are equal. Enter a description
Input DescriptionEnter only one row with two floating-point output descriptions
Output DescriptionOutput only one row, or output yes if equal, otherwise output no. Sample input
Sample Input2.980000001 2.9800000000001 Sample Output
Sample OutputYes data range and hints
Data Size & HintWe generally consider two floating-point numbers to be equal when and when the error between them does not exceed 1e-8. Idea: The two floating-point numbers in the form of a string to the character array, and then according to one of the loop judgment, note that the error is not more than 1e-8, that is, to find the location of the decimal point. Record the decimal position with flag and then determine whether the error is not more than 1e-8 according to flag. Code:
#include <stdio.h>
#include <string.h>
int main ()
{
Char a[20],b[20];
int i,p,flag=0;
scanf ("%s%s", b);
P=strlen (a);
for (i=0;i<p;i++)
{
if (a[i]== '. ')
{
Flag==i;
}
if (A[i]!=b[i])
{
if (i<flag+7)
{
Break
}
Else
{
if (i==flag+7)
{
if (a[i]-b[i]-' 0 '-' 0 ' >1| | a[i]-b[i]-' 0 '-' 0 ' <-1)
{
Break
}
}
}
}
}
if (i==p)
{
printf ("yes\n");
}
Else
{
printf ("no\n");
}
return 0;
}
1203 determining whether floating-point numbers are equal