news 2026/5/24 7:07:04

信息学奥赛 取整技巧

作者头像

张小明

前端开发工程师

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/5/22 1:47:33

AI视频创作不再踩坑:HunyuanVideo版权合规实战指南

你是不是也有这样的困惑&#xff1a;用AI生成的视频明明很惊艳&#xff0c;却总担心会不会侵权&#xff1f;投入大量时间制作的内容&#xff0c;会不会因为版权问题被下架&#xff1f;别担心&#xff0c;今天我们就来聊聊如何用HunyuanVideo安全合规地创作AI视频&#xff0c;让…

作者头像 李华
网站建设 2026/5/22 22:40:54

lazygit完整日志监控实战指南:高效追踪Git操作全流程

lazygit完整日志监控实战指南&#xff1a;高效追踪Git操作全流程 【免费下载链接】lazygit 一个简化的终端用户界面&#xff0c;用于执行Git命令&#xff0c;旨在提高开发者使用Git的效率和体验。 项目地址: https://gitcode.com/GitHub_Trending/la/lazygit lazygit作为…

作者头像 李华
网站建设 2026/5/19 23:56:28

Navicat重置工具:macOS试用期延长终极指南 [特殊字符]

Navicat重置工具&#xff1a;macOS试用期延长终极指南 &#x1f680; 【免费下载链接】navicat_reset_mac navicat16 mac版无限重置试用期脚本 项目地址: https://gitcode.com/gh_mirrors/na/navicat_reset_mac 还在为Navicat试用期到期而烦恼吗&#xff1f;这款专为mac…

作者头像 李华
网站建设 2026/5/22 13:59:43

Windows Defender移除工具:2025年系统优化终极指南

Windows Defender移除工具&#xff1a;2025年系统优化终极指南 【免费下载链接】windows-defender-remover 项目地址: https://gitcode.com/gh_mirrors/win/windows-defender-remover 你是否曾经在安装软件时被Windows Defender频繁拦截&#xff1f;是否因为系统安全组…

作者头像 李华
网站建设 2026/5/18 21:54:11

MTK设备解锁终极指南:简单三步绕过bootrom保护

还在为MTK设备的限制而苦恼吗&#xff1f;这款强大的MTK解锁工具能够帮你轻松绕过bootrom保护&#xff0c;释放手机的全部潜力&#xff01;无论你是想进行深度刷机还是解锁隐藏功能&#xff0c;这个简单易用的手机安全工具都能为你提供专业级解决方案。 【免费下载链接】bypass…

作者头像 李华