news 2026/6/11 8:17:34

信息学奥赛 取整技巧

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
信息学奥赛 取整技巧

1.基本取整函数

向下取整(Floor)

#include <cmath> int a = floor(3.7); // 3 int b = floor(-3.7); // -4
  • 向负无穷方向取整

  • 对于正数:去掉小数部分

  • 对于负数:去掉小数部分再减1

向上取整(Ceil)

int a = ceil(3.2); // 4 int b = ceil(-3.2); // -3
  • 向正无穷方向取整

  • 对于正数:去掉小数部分加1

  • 对于负数:去掉小数部分

四舍五入(Round)

int a = round(3.4); // 3 int b = round(3.5); // 4 int c = round(-3.5); // -4

2.整数除法取整技巧

向下取整

int a = 7 / 3; // 2,自动向下取整 int b = -7 / 3; // -2(C++中向0取整)

向上取整公式

// 正整数a,b向上取整 int ceil_div(int a, int b) { return (a + b - 1) / b; } // 例子 int x = (7 + 3 - 1) / 3; // 3 int y = (8 + 3 - 1) / 3; // 3 int z = (9 + 3 - 1) / 3; // 4

通用向上取整

int ceil_div(int a, int b) { if (a % b == 0) return a / b; return a / b + 1; }

3.取模运算技巧

确保非负余数

int mod_positive(int a, int b) { int r = a % b; if (r < 0) r += b; return r; }

循环下标技巧

// 循环访问数组 int arr[10]; for (int i = 0; i < 100; i++) { int index = i % 10; // 0,1,2,...,9,0,1,2,... // 使用 arr[index] } // 循环星期 int x = 3; // 周三开始 for (int i = 0; i < n; i++) { int weekday = (x + i) % 7; // 0-6 if (weekday == 0) weekday = 7; // 转为1-7 }

4.常用数学公式

分组数量计算

// n个元素,每组m个,需要多少组? int groups = (n + m - 1) / m; // 向上取整 // 例子:10个苹果,每盒装3个,需要几盒? int boxes = (10 + 3 - 1) / 3; // 4

范围映射

// 将value从[old_min, old_max]映射到[new_min, new_max] int map_value(int value, int old_min, int old_max, int new_min, int new_max) { int old_range = old_max - old_min; int new_range = new_max - new_min; return ((value - old_min) * new_range + old_range/2) / old_range + new_min; }

5.特殊技巧

判断整除

bool is_divisible(int a, int b) { return a % b == 0; }

获取个位数字

int last_digit = n % 10;

去掉个位数字

int without_last = n / 10;

判断奇偶

bool is_even = n % 2 == 0; bool is_odd = n % 2 == 1; // 或 n & 1

6.循环队列/缓冲区索引

#define SIZE 10 int buffer[SIZE]; int head = 0, tail = 0; // 添加元素 buffer[tail] = value; tail = (tail + 1) % SIZE; // 自动循环 // 获取元素 int val = buffer[head]; head = (head + 1) % SIZE;

7.日期/星期计算技巧

// 计算n天后是星期几 int weekday_after(int current_weekday, int days_later) { return (current_weekday + days_later - 1) % 7 + 1; } // 简化版(题目中使用) int weekday = (x + i) % 7; // 0=周日,1=周一,...,6=周六

8.二进制位操作技巧

向上取整到2的幂

int next_power_of_two(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; }

9.实际应用示例

分页显示

int total_items = 100; int items_per_page = 10; int total_pages = (total_items + items_per_page - 1) / items_per_page; // 当前页数据范围 int page = 3; int start = (page - 1) * items_per_page; int end = min(page * items_per_page, total_items);

网格坐标

// 一维转二维 int index = 15; // 一维索引 int rows = 4, cols = 4; int row = index / cols; // 行号 int col = index % cols; // 列号 // 二维转一维 int new_index = row * cols + col;

记忆要点:

  1. 除法默认向0取整(截断小数)

  2. 向上取整(a+b-1)/b

  3. 取模结果符号与被除数相同(C++特性)

  4. 负数取整要特别注意边界情况

  5. 循环索引用%实现自动回绕

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/11 19:19:55

如何彻底解决Mac过热问题?Turbo Boost Switcher让你的电脑重获新生

如何彻底解决Mac过热问题&#xff1f;Turbo Boost Switcher让你的电脑重获新生 【免费下载链接】Turbo-Boost-Switcher Turbo Boost disabler / enable app for Mac OS X 项目地址: https://gitcode.com/gh_mirrors/tu/Turbo-Boost-Switcher 你是否曾经在剪辑视频时听到…

作者头像 李华
网站建设 2026/6/11 15:18:31

SDCAlertView:iOS开发者的终极对话框解决方案

SDCAlertView&#xff1a;iOS开发者的终极对话框解决方案 【免费下载链接】SDCAlertView The little alert that could 项目地址: https://gitcode.com/gh_mirrors/sd/SDCAlertView 在日常iOS应用开发中&#xff0c;对话框和操作菜单是用户交互的重要组成部分。然而&…

作者头像 李华
网站建设 2026/6/10 15:32:00

终极Revit族库:2万+免费BIM资源一键获取

探索BIM设计的无限可能&#xff01;我们为您精心准备了2万个免费Revit族库资源&#xff0c;这些高质量的BIM资源经过专业分类整理&#xff0c;支持一键获取&#xff0c;将极大提升您的设计效率。无论是建筑初学者还是中级用户&#xff0c;都能轻松上手&#xff0c;快速掌握高效…

作者头像 李华
网站建设 2026/6/10 19:32:40

联想拯救者BIOS隐藏设置一键解锁:释放硬件全部潜能

联想拯救者BIOS隐藏设置一键解锁&#xff1a;释放硬件全部潜能 【免费下载链接】LEGION_Y7000Series_Insyde_Advanced_Settings_Tools 支持一键修改 Insyde BIOS 隐藏选项的小工具&#xff0c;例如关闭CFG LOCK、修改DVMT等等 项目地址: https://gitcode.com/gh_mirrors/le/L…

作者头像 李华
网站建设 2026/6/9 15:24:34

25、计算机安全事件报告指南

计算机安全事件报告指南 在当今数字化的时代,计算机安全事件时有发生,及时、准确地报告这些事件对于维护网络安全至关重要。本文将为你详细介绍如何向合适的机构报告安全事件,以及在报告过程中需要注意的事项。 问题提出 当你遭遇安全事件,如系统被黑客攻击、检测到可疑…

作者头像 李华
网站建设 2026/6/11 18:50:18

KEA DHCP服务器企业级部署与性能优化指南

KEA DHCP服务器企业级部署与性能优化指南 【免费下载链接】kea A modern, scalable, robust DHCPv4 and DHCPv6 server, with database (MySQL, PostgreSQL), hooks, multi-threading, RADIUS, NETCONF, Kerberos and more. 项目地址: https://gitcode.com/gh_mirrors/kea/ke…

作者头像 李华