将除法结果汇总到c中的下一个整数

我创建代码以显示多个页面(最多5行/页)与一个列表中的人:

/* PRE: page : number of the page we want to show, starting with 1 * RETURNS: pagenumber of the page showing if there is one, 0 otherwise */ const int buf_length = 255; const int max_num_lines = 15; const int num_person_per_page = max_num_lines / 3; const int num_person = person_get_num_person(personmgr); char buf[buf_length+1]; int i, count, cur = 0; /* List Header */ snprintf(buf, buf_length, "List of person on page (%d/%d)):", page, num_person/num_person_per_page); list_set_text( list, cur++, buf); list_set_hilight(list, -1); 

如果列表中的人数不是5的倍数(在我的示例中为72),则最后一页的列表标题将页面总数返回为14而不是15(14/15)。

首页列表标题:

 List of person on page: 1/14: 01. AAA 02. BBB 03. CCC 04. DDD 05. EEE 

第二页列表标题:

 List of person on page: 2/14: 06. FFF 07. GGG 08. HHH ........................ 

最后一页列表标题:

 List of person on page: 14/15: 71. XXX 72. ZZZ 

我想要舍入到下一个整数(要正确显示的页码)。

 72 / 5 = 14.4 => 15 70 / 5 = 14 => 14 36 / 5 = 7.2 => 8 

首页列表标题:

 List of person on page: 1/15: 01. AAA 02. BBB 03. CCC 04. DDD 05. EEE 

第二页列表标题:

 List of person on page: 2/15: 06. FFF 07. GGG 08. HHH ........................ 

最后一页列表标题:

 List of person on page: 15/15: 71. XXX 72. ZZZ 

您可以编写(n + 4) / 5来整体计算n / 5的数学上限:如果n已经是5的倍数,那么您将添加4 / 5 == 0 ,否则您将添加1

包含math.h文件并使用其ceil()函数。

另一种方法:

 (num_person/num_person_per_page) + ((num_person % num_person_per_page) ? 1 : 0); 

也许更容易理解。 如果模数不为零,则加1。