news 2026/5/26 1:40:38

信息学奥赛 取整技巧

作者头像

张小明

前端开发工程师

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/24 8:33:57

效率提升40%:HiDream-I1_ComfyUI插件重构AI图像创作流程

效率提升40%&#xff1a;HiDream-I1_ComfyUI插件重构AI图像创作流程 【免费下载链接】HiDream-I1_ComfyUI 项目地址: https://ai.gitcode.com/hf_mirrors/Comfy-Org/HiDream-I1_ComfyUI 导语 ComfyUI生态再添新成员——HiDream-I1插件正式发布&#xff0c;通过模块化节…

作者头像 李华
网站建设 2026/5/25 20:53:56

Wan2.2-T2V-A14B能否生成带有字幕的视频内容?

Wan2.2-T2V-A14B能否生成带有字幕的视频内容&#xff1f; 在短视频、在线教育和跨文化传播日益依赖自动化内容生成的今天&#xff0c;一个实际而关键的问题浮出水面&#xff1a;AI生成的视频能否“自带”字幕&#xff1f; 更具体地说&#xff0c;像阿里巴巴推出的旗舰级文本到视…

作者头像 李华
网站建设 2026/5/25 12:12:46

DBeaver终极指南:从零开始掌握数据库管理工具

还在为复杂的数据库操作而烦恼吗&#xff1f;DBeaver作为一款强大的开源数据库管理工具&#xff0c;能够帮助你轻松应对各种数据操作需求。本指南将带你从安装配置到高级应用&#xff0c;全面掌握这款数据库管理神器。 【免费下载链接】dbeaver 项目地址: https://gitcode.c…

作者头像 李华
网站建设 2026/5/25 12:46:52

Double Take:一站式人脸识别管理平台的终极解决方案

Double Take&#xff1a;一站式人脸识别管理平台的终极解决方案 【免费下载链接】double-take Unified UI and API for processing and training images for facial recognition. 项目地址: https://gitcode.com/gh_mirrors/dou/double-take 在当今数字化时代&#xff0…

作者头像 李华
网站建设 2026/5/25 18:18:29

Wan2.2-T2V-A14B模型的错误恢复与断点续生功能

Wan2.2-T2V-A14B模型的错误恢复与断点续生功能 在影视预演、广告创意和虚拟内容工厂等高要求场景中&#xff0c;AI生成视频已不再是“能不能做”的问题&#xff0c;而是“能不能稳定地做出来”的问题。一个60秒、720P分辨率、运动自然的AI生成视频&#xff0c;可能需要近一个小…

作者头像 李华
网站建设 2026/5/26 5:34:12

零成本企业安全监控:开源SOC平台搭建完全指南

零成本企业安全监控&#xff1a;开源SOC平台搭建完全指南 【免费下载链接】SOC-OpenSource This is a Project Designed for Security Analysts and all SOC audiences who wants to play with implementation and explore the Modern SOC architecture. 项目地址: https://g…

作者头像 李华