1
Programming, learning to use type conversion operators static_cast (10 points)
Topic Difficulty: Easy
Topic content:
program, read in two numbers, and then calculate the two-digit division result. The first number read in A is an integer; the second number read in B is a double-precision floating point;
Converts b to an integer, then computes the integer division A/b, with the result recorded as X;
Converts a to a double-precision floating-point number, and then calculates a double-precision floating-point number Division A/b, with the result recorded as Y
Converts a to a double-precision floating-point number, converts B to an integer and then converts to a double-precision floating -point number, and then calculates a double-precision floating-point number Division A/b, with the result recorded as Z
Outputs the values of x, Y, Z.
This procedure does not need to consider the divisor is 0, the result overflow and so on abnormal situation .
Attention:
All of the above conversions use the static_cast operator
If the division result is a floating-point number, the output is accurate to 3 digits after the decimal point
Input format:
The first number is an integer, the second is a double-precision floating-point number;
Use a space between two numbers
Output format:
sequentially outputs x, y, z values, separated by 1 spaces between adjacent two values
Note 1: If the output value is a floating-point number, you need to use the std::fixed with the Std::setprecision function to set the decimal point to 3 bits.
These two STD members can search by themselves or go to cppreference.com to retrieve (English)
Note 2: Use the Std::setprecision function to include the <iomanip> header file
Input Sample:
4 2.5
Sample output:
2 1.600 2.000
time limit: 500ms memory limit: 32000kb
#include <iostream> #include <iomanip>int main () {int a;double b;std::cin >> a;std::cin >> b;int x = a/static_cast<int> (b);//convert B to an integer and then calculate integer division A/b, which is recorded as xdouble y = static_cast<double> (a)/b;//converts a to a double-precision floating-point number, but After calculating double-precision floating-point number Division A/b, the result is recorded as ydouble z = static_cast<double> (a)/static_cast<double> (static_cast<int> (b)) ,//Convert A to a double-precision floating-point number, convert B to an integer and then convert to a double-precision floating-point number, and then calculate the double-precision floating-point division A/b, with the result zstd::cout << x << "<< std::fixed << Std::setprecision (3) << y << "" << z << std::endl;return 0;}
Introduction to NetEase Cloud classroom _c++ Programming (UP) _ Unit 3rd: The higher level-Beyond the C Syntax _ 3rd unit Job "2"-online programming (difficulty: easy; 10 points)