[Làm Quen OJ]. Bài 8. Phép chia

View as PDF

Submit solution


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

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

Trong ngôn ngữ lập trình như C, C++, Java khi bạn sử dụng 2 số nguyên để chia cho nhau thì kết quả của phép chia đó chỉ dữ lại phần nguyên cho dù bạn có để kết quả ở số thực như float hay double. Ví dụ a = 10, b = 3 thì phép chia a / b sẽ có kết quả là 3 thay vì 3.3333, để lấy được phần thập phân khi chia 2 số nguyên cho nhau bạn cần thực hiện ép kiểu a hoặc b, hoặc cả 2 và b sang dạng số thực trước khi chia. Ví dụ float c = (float) a / b thì khi đó c = 3.3333


Đầu vào

1 dòng duy nhất chứa lần lượt 2 số nguyên b và a


Ràng buộc

1<=a,b<=1000


Đầu ra

Dòng 1 in ra thương của a / b khi sử dụng phép chia nguyên; Dòng 2 in ra thương của a / b khi sử dụng phép chia lấy phần thập phân với độ chính xác 2 số sau dấu phẩy.


Ví dụ

Input 01
30 70
Output 01
2
2.33

Comments

Please read the guidelines before commenting.



  • 0
    hh1c6789  commented on Dec. 20, 2025, 9:41 a.m.

    bị lừa rồi =)))), ko phải a/b đâu mà là b/a mới đúng


    • 0
      pycpp  commented on Dec. 28, 2025, 12:09 p.m.

      a chia b mà, nhưng mà nhập b trước


  • -1
    L8_DuongThanhKhiem  commented on Nov. 22, 2025, 4:01 a.m.

    FULL AC:

    #include <bits/stdc++.h>
    using namespace std;
    int a, b;
    int main(){
        cin >> b >> a;
        cout << a / b << endl;
        cout << fixed << setprecision(2) << (float)a / b;
        return 0;
    }
    

  • 0
    trantruongphumy0607  commented on Aug. 4, 2025, 2:32 a.m.

    Cu Lua The Ki :)) Van Cu CIN>>A>>B ROi LAY B/A THOI :)))


  • 0
    bengokyeuanh99  commented on June 20, 2025, 6:40 a.m. edit 2

    include <stdio.h>

    int printdivisionresult(int numerator, int denominator) { if (denominator == 0) { fprintf(stderr, "Math error: Cannot divide by zero.\n"); return 1; }

    printf("%d\n", numerator / denominator);
    printf("%.2f\n", (double)numerator / denominator);
    return 0;
    

    }

    int main() { int numerator, denominator;

    if (scanf("%d %d", &denominator, &numerator) != 2) {
        fprintf(stderr, "Input error.\n");
        return 1;
    }
    
    return print_division_result(numerator, denominator);
    

    }


  • -1
    pvhung  commented on Nov. 25, 2024, 10:40 a.m.

    mn nhập b xong mới nhập a nhé cin >> b >> a;


  • -1
    hoang_trong_nhan_cpp  commented on June 7, 2024, 4:51 p.m.

    bai keu nhap b truosc a sau nha cac ban


  • -1
    Nah_I_am_Nam  commented on May 16, 2024, 3:52 a.m.

    cái này phải là b/a mới đúng