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)

Source: Internet
Author: User
Tags integer division

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;

    1. Converts b to an integer, then computes the integer division A/b, with the result recorded as X;

    2. 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

    3. 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:

    1. All of the above conversions use the static_cast operator

    2. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.