news 2026/8/16 6:20:58

信息学奥赛 取整技巧

作者头像

张小明

前端开发工程师

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/8/12 13:36:04

BiliBiliCCSubtitle:终极B站字幕下载与格式转换解决方案

BiliBiliCCSubtitle&#xff1a;终极B站字幕下载与格式转换解决方案 【免费下载链接】BiliBiliCCSubtitle 一个用于下载B站(哔哩哔哩)CC字幕及转换的工具; 项目地址: https://gitcode.com/gh_mirrors/bi/BiliBiliCCSubtitle 还在为B站视频的字幕无法离线使用而困扰吗&am…

作者头像 李华
网站建设 2026/8/13 15:46:30

从零开始:在Windows系统上搭建酷安社区桌面化体验的完整实践指南

从零开始&#xff1a;在Windows系统上搭建酷安社区桌面化体验的完整实践指南 【免费下载链接】Coolapk-Lite 一个基于 UWP 平台的第三方酷安客户端精简版 项目地址: https://gitcode.com/gh_mirrors/co/Coolapk-Lite 还在为在电脑上使用安卓模拟器体验酷安社区而烦恼吗&…

作者头像 李华
网站建设 2026/8/13 14:29:48

DBeaver数据迁移终极指南:跨数据库同步的完整解决方案

DBeaver数据迁移终极指南&#xff1a;跨数据库同步的完整解决方案 【免费下载链接】dbeaver 项目地址: https://gitcode.com/gh_mirrors/dbe/dbeaver 还在为不同数据库系统间的数据迁移而烦恼吗&#xff1f;DBeaver作为一款开源的通用数据库管理工具&#xff0c;其强大…

作者头像 李华
网站建设 2026/8/15 19:52:59

薄膜光学与镀膜技术高清PDF完整学习指南

为什么选择这个高清PDF资源&#xff1f; 【免费下载链接】薄膜光学与镀膜技术高清版PDF分享 薄膜光学与镀膜技术 高清版PDF 项目地址: https://gitcode.com/Open-source-documentation-tutorial/c516c 在光学工程和薄膜技术领域&#xff0c;找到一本清晰度高、内容完整的…

作者头像 李华
网站建设 2026/8/16 8:54:57

TripoSR实战宝典:5秒从图片到专业3D模型的完整攻略

TripoSR实战宝典&#xff1a;5秒从图片到专业3D模型的完整攻略 【免费下载链接】TripoSR 项目地址: https://gitcode.com/GitHub_Trending/tr/TripoSR 想要将普通照片瞬间变成立体模型吗&#xff1f;TripoSR这款革命性的开源3D重建工具&#xff0c;让每个人都能在短短5…

作者头像 李华
网站建设 2026/8/15 11:02:00

Mirai Console Loader 深度使用与定制化配置指南

Mirai Console Loader 深度使用与定制化配置指南 【免费下载链接】mirai-console-loader 模块化、轻量级且支持完全自定义的 mirai 加载器。 项目地址: https://gitcode.com/gh_mirrors/mi/mirai-console-loader 请基于以下要求&#xff0c;创作一篇关于Mirai Console L…

作者头像 李华