当前位置:网站首页>Noi OJ 1.3 05: floating point numeric C language for calculating fractions

Noi OJ 1.3 05: floating point numeric C language for calculating fractions

2022-06-23 10:29:00 chd44

describe

Two integers a and b As numerator and denominator respectively , Existing score a/b , Find its floating-point value ( Double precision floating point , After decimal point 9 position )

Input

Enter only one line , Includes two integers a and b(b Not for 0)

Output

The output is only one line , fraction a/b The floating-point value of ( Double precision floating point , After decimal point 9 position )

There are two solutions to the problem

The first is to set the input number to double The type of , So the number after rounding out will also be double Type of ( as follows )

#include<stdio.h>
int main(){
double a,b;
scanf("%lf %lf",&a,&b);
printf("%.9lf",a/b);
return 0;
}

The second can be seen as a cast

#include<stdio.h>
int main(){
int a,b;
scanf("%d %d",&a,&b);
double c=a*1.0/b;
printf("%.9lf",c);
return 0;
}

notes : there   a  must   *1.0, If direct a/b Words , Both are int The integer of , So the number that comes out of the division must be an integer , But if we   *1.0, So we give integers a new type , You can go to double Divide by ( At this time a*1.0 Can be seen as a new double Number of types ).

原网站

版权声明
本文为[chd44]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231012356627.html