news 2026/6/9 20:14:24

信息学奥赛 取整技巧

作者头像

张小明

前端开发工程师

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/9 15:42:40

Driver.js 1.x 版本全面升级指南:从旧版到新架构的平滑迁移

Driver.js 1.x 版本全面升级指南&#xff1a;从旧版到新架构的平滑迁移 【免费下载链接】driver.js driver.js - 一个轻量级、无依赖的纯 JavaScript 库&#xff0c;用于控制用户在网页上的焦点移动&#xff0c;适用于需要实现网页交互和用户指引的前端开发者。 项目地址: ht…

作者头像 李华
网站建设 2026/6/8 14:56:50

Nuke视觉特效终极指南:200+专业工具一键部署方案

Nuke视觉特效终极指南&#xff1a;200专业工具一键部署方案 【免费下载链接】NukeSurvivalToolkit_publicRelease public version of the nuke survival toolkit 项目地址: https://gitcode.com/gh_mirrors/nu/NukeSurvivalToolkit_publicRelease 在视觉特效制作领域&am…

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

Wan2.2-T2V-A14B模型更新后向兼容性测试报告

Wan2.2-T2V-A14B模型更新后向兼容性测试报告 在AIGC从“能用”迈向“好用”的关键阶段&#xff0c;文本到视频&#xff08;Text-to-Video, T2V&#xff09;技术正经历一场静默而深刻的变革。曾经被视为实验性质的生成模型&#xff0c;如今已逐步进入影视预演、广告自动化、数字…

作者头像 李华
网站建设 2026/6/7 17:46:25

用Wan2.2-T2V-A14B做品牌宣传片可行吗?实测告诉你

用Wan2.2-T2V-A14B做品牌宣传片可行吗&#xff1f;实测告诉你 在品牌营销节奏越来越快的今天&#xff0c;一条新品宣传片从创意构思到上线发布&#xff0c;动辄需要一周甚至更久。拍摄档期、场地协调、后期制作层层卡点&#xff0c;而市场热点却稍纵即逝。有没有可能&#xff0…

作者头像 李华
网站建设 2026/6/9 23:32:51

如何提高微信小游戏分享转化率?试试这7个接口

点击上方亿元程序员关注和★星标 引言 哈喽大家好&#xff0c;好久不见&#xff0c;由于素材和正反馈不足&#xff0c;一不小心就断更了。 最近有很多小伙伴新上线了小游戏&#xff0c;看到群里五花八门的分享链接&#xff1a; 不知道小伙伴们看到这样的链接会不会点进去体验…

作者头像 李华