[Làm quen OJ]. Bài 6. Hàm ceil, floor, round

View as PDF

Submit solution


Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Problem source:
28Tech
Problem type
Allowed languages
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Hàm ceil : làm tròn lên số nguyên gần nhất, floor : làm tròn xuống số nguyên gần nhất, round : làm tròn số nguyên phụ thuộc vào phần thập phân.

Cho số thực X nhiệm vụ của bạn là sử dụng 3 hàm trên để tìm số nguyên nhỏ hơn gần X nhất, số nguyên lớn hơn gần X nhất, số nguyên gần X nhất.

Lưu ý bạn cần in ra số nguyên nên không được in trực tiếp giá trị của 3 hàm này, nó sẽ là số thực.


Đầu vào
  • Dòng duy nhất chứa số thực X

Ràng buộc
  • 1<=X<=10^6

Đầu ra
  • In ra 3 số trên 3 dòng

Ví dụ

Input 01
3.59
Output 01
3
4
4

Comments

Please read the guidelines before commenting.



  • 0
    Huu_Tien  commented on Aug. 5, 2026, 1:55 a.m.

    include <iostream>

    include <math.h>

    using namespace std;

    int main () { double x; cin >> x; cout << floor(x) << endl << ceil(x) << endl << round(x) << endl; return 0;

    } mọi người giúp em tại sao phải ép int cho từng cái hàm v ạ. Em không biết lí do vì sao hết, mọi người giúp em giải thích với ạ. Em cảm ơn mọi người nhiều lắm


    • 0
      vyctini0607  commented on Sept. 11, 2026, 8:08 a.m.

      do you understand it now ma friend ?


  • 0
    Huu_Tien  commented on Aug. 5, 2026, 1:54 a.m.

    include <iostream>

    include <math.h>

    using namespace std;

    int main () { float x; cin >> x; cout << floor(x) << endl << ceil(x) << endl << round(x) << endl; return 0;

    } mọi người giúp em với em coi lời giải thì phải ép int cho từng cái, không biết tại sao nữa. Mọi người giúp em giải thích với ạ, em cảm ơn mọi người nhiều lắm.


  • 0
    nonamexxx  commented on Jan. 9, 2026, 1:05 p.m.

    import java.util.Scanner;

    public class Bai1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x = sc.nextDouble();
        System.out.println((int)Math.floor(x));
        System.out.println((int)Math.ceil(x));
        System.out.println((int)Math.round(x));
    }
    

    }


  • 0
    L8_DuongThanhKhiem  commented on Nov. 22, 2025, 3:59 a.m.

    FULL AC:

    #include <bits/stdc++.h>
    using namespace std;
    double x;
    int main(){
        cin >> x;
        long long tron = floor(x);
        long long tron2 = ceil(x);
        long long round1 = round(x);
        cout << tron << endl;
        cout << tron2 << endl;
        cout << round1;
        return 0;
    }
    

  • 0
    Nguyen0907  commented on July 5, 2025, 1:29 p.m.

    include <iostream>

    include <cmath>

    using namespace std;

    int main() { double x;

    cin >> x;
    
    long long tron = floor(x);
    long long tron2 = ceil(x);
    long long round1 = round(x);
    
    cout << tron << '\n'
         << tron2 << '\n'
         << round1;
    

    }


  • 0
    SteepMoleCules  commented on July 1, 2025, 2:30 p.m.

    gợi ý nhé: ae cần khai báo biến double và ép kiểu giá trị int cho từng kết quả


  • 2
    nmtuong_cpp  commented on April 16, 2025, 3:29 a.m.

    Bài này số thực x mọi người dùng double nhé, dùng float rớt mấy case đằng sau.

    Mình thử thì không ép int nó vẫn tự cho về số nguyên, điều kiện là x bé hơn 10^6. x >= 10^6 thì nó ra số dạng m.n*e + p


    • 0
      luuthuyvan_cpp  commented on Feb. 25, 2026, 6:08 p.m.

      ui cảm ơn bn


    • 0
      Quanghuybm2k6  commented on May 22, 2025, 10:15 a.m.

      cảm ơn người anh em đã cho mình ít mẹo


  • 0
    bengokyeuanh99  commented on April 9, 2025, 4:22 a.m.

    import sys import math

    def read_float(): line = sys.stdin.readline() try: return float(line.strip()) except ValueError: raise ValueError("Invalid input: not a float.")

    def roundhalfup(x): return int(x + 0.5) if x >= 0 else int(x - 0.5)

    def main(): try: x = readfloat() print(int(math.floor(x))) print(int(math.ceil(x))) print(roundhalf_up(x)) except Exception as e: sys.stderr.write(f"Error: {e}\n") sys.exit(1)

    if name == "main": main()


  • 0
    tienduc_381  commented on April 9, 2025, 2:02 a.m.

    include <iostream>

    using namespace std; int floor(double x){ if ((double)x == (int)x){ return x; }else{ return (int)x ; } } int ceil(double x ){ if ((double)x == (int)x){ return x; }else{ return (int)x +1 ; } } int round(double x){ if ((double)x == (int)x){ return x; }else if(x-(int)x<0.5){ return (int)x ; }else{ return (int)x+1; } } int main(){ double x; cin >> x ; cout << floor(x) << endl; cout << ceil(x) << endl; cout << round(x) << endl ; }


  • -10
    le_anh_khoa2006  commented on Nov. 5, 2024, 8:18 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -24
    longhk2012  commented on May 26, 2024, 12:13 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


    • -21
      nguyen_quoc_cuonghihi  commented on June 4, 2024, 4:41 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.