We want to round off x, a float, to an int value, The correct way to do is:

We want to round off x, a float, to an int value, The correct way to do is:

A.  y = (int)(x + 0.5)

B.  y = int(x + 0.5)

C. y = (int)x + 0.5

D. y = (int)((int)x + 0.5)

Hướng dẫn

Chọn A là đáp án đúng

Xem lời giải

Xem lời giải

Round off ở đây có nghĩa là làm tròn số thập phân.

Ví dụ: 3.45 –> 3, 3.5 –> 4.

Do vậy, ở đây để làm tròn x ta cộng thêm 0.5.

Đáp án B cũng có thể đúng, nhưng đáp án A là chuẩn nhất, đảm bảo đúng trên mọi compiler.

 

Thư viện tài liệu12 Tháng bảy, 2023 @ 2:55 chiều

Đánh giá chủ đề này
How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site