c#做网站,成都设计公司招聘,wordpress 做表格,企业网站的首页1. 基本取整函数向下取整#xff08;Floor#xff09;#include cmath
int a floor(3.7); // 3
int b floor(-3.7); // -4向负无穷方向取整对于正数#xff1a;去掉小数部分对于负数#xff1a;去掉小数部分再减1向上取整#xff08;Ceil#xff09;int a ce…1.基本取整函数向下取整Floor#include cmath int a floor(3.7); // 3 int b floor(-3.7); // -4向负无穷方向取整对于正数去掉小数部分对于负数去掉小数部分再减1向上取整Ceilint a ceil(3.2); // 4 int b ceil(-3.2); // -3向正无穷方向取整对于正数去掉小数部分加1对于负数去掉小数部分四舍五入Roundint a round(3.4); // 3 int b round(3.5); // 4 int c round(-3.5); // -42.整数除法取整技巧向下取整int a 7 / 3; // 2自动向下取整 int b -7 / 3; // -2C中向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 16.循环队列/缓冲区索引#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;记忆要点除法默认向0取整截断小数向上取整(ab-1)/b取模结果符号与被除数相同C特性负数取整要特别注意边界情况循环索引用%实现自动回绕