溫馨提示×

利用C++ ceil()函數解決實際編程問題

c++
小樊
106
2024-09-10 18:33:14
欄目: 編程語言

ceil() 函數是 C++ 標準庫 <cmath> 中的一個函數,用于計算大于或等于給定浮點數的最小整數

  1. 計算某個數的上界:
#include<iostream>
#include <cmath>

int main() {
    double num = 5.6;
    int upper_bound = std::ceil(num);
    std::cout << "Upper bound of "<< num << " is: "<< upper_bound<< std::endl;
    return 0;
}
  1. 計算兩個數之間的差值的上界:
#include<iostream>
#include <cmath>

int main() {
    double num1 = 7.3;
    double num2 = 2.8;
    double difference = num1 - num2;
    int upper_bound = std::ceil(difference);
    std::cout << "Upper bound of the difference between "<< num1 << " and "<< num2 << " is: "<< upper_bound<< std::endl;
    return 0;
}
  1. 在分頁應用中,根據每頁顯示的項目數和總項目數計算總頁數:
#include<iostream>
#include <cmath>

int main() {
    int total_items = 45;
    int items_per_page = 10;
    int total_pages = std::ceil(static_cast<double>(total_items) / items_per_page);
    std::cout << "Total pages: "<< total_pages<< std::endl;
    return 0;
}

這些示例展示了如何使用 ceil() 函數解決實際編程問題。請注意,在使用 ceil() 函數時,確保包含 <cmath> 頭文件。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女