Integer Replacement 整数替换

本文探讨了一个有趣的数学问题——整数替换。给定一个正整数n,通过递归算法找到将其转换为1所需的最少步骤。算法巧妙地利用了位运算优化计算过程,并附带了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个正整数 n,你可以做如下操作:

1. 如果 是偶数,则用 n / 2替换 n
2. 如果 是奇数,则可以用 n + 1n - 1替换 n
变为 1 所需的最小替换次数是多少?

示例 1:

输入:
8

输出:
3

解释:
8 -> 4 -> 2 -> 1

示例 2:

输入:
7

输出:
4

解释:
7 -> 8 -> 4 -> 2 -> 1
或
7 -> 6 -> 3 -> 2 -> 1

思路:这道题用递归来做,边界条件:当n等于0,返回0。否则当n为偶数时,返回1+递归(n/2),当n为奇数,返回1+min(递归(n+1)和递归(n-1))的最小值,但是这样对于最大值INT_MAX会报错,那么我们换种想法,当n为奇数时,反正n+1或者n-1都是偶数,下一步也是返回1+递归(n/2),那么我们不如直接跳两步,刚好可以解决最大数的问题,所以当n为奇数时,返回2+min(递归((n+1)/2),递归((n-1)/2))。

参考代码:

class Solution {
public:
    int integerReplacement(int n) {
	if (n == 1) return 0;
	else if ((n & 1) == 0) return (1 + integerReplacement(n >> 1));
	else {
		long long t = n;
		return 2 + min(integerReplacement((t + 1)>>1), integerReplacement((t - 1)>>1));
	}   
    }
};

 

 

以下是代码要求和代码,代码要求;输入一个32位整数,实现下面功能的转换函数unsigned int pattern_replace(unsigned int input_int),从高到低将此整数二进制码中的101替换成011,将替换后结果作为返回值返回。注意:该替换必须要直接在整数上进行模式的识别和替换,不能转成字符或整形数组;已替换的部分直接跳过,处理余下部分即可,如10101替换成01101而不是01011;要求完全使用bit操作进行判断、替换。代码:/* Copyright(c) 2025 Shenzhen TP-LINK Technologies Co.Ltd. * * file work1.c * brief This is a sample of coding criterion for pattern_replace. * * author Lin Zihao * version 1.0.1 * date 25Jul31 * * history \arg 1.0.1, 25Jul31, Lin Zihao, Create the file. */ #include <stdint.h> #include <stdio.h> /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ #define BIT_LENGTH 32 // Define the bit length /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* EXTERN_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_PROTOTYPES */ /**************************************************************************************************/ static uint32_t pattern_replace(uint32_t input_int); /**************************************************************************************************/ /* VARIABLES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_FUNCTIONS */ /**************************************************************************************************/ /* * Function: pattern_replace * Purpose: Replaces every occurrence of the bit pattern '101' in the input with '011'. * Parameters: * input_int - A 32-bit unsigned integer in which the bit pattern replacement is performed. * Return value: * A 32-bit unsigned integer with all instances of '101' replaced by '011'. */ static uint32_t pattern_replace(uint32_t input_int) { uint32_t result = 0; /* Used to store the processed result */ /* Loop from the most significant bit (bit 31) to the least significant bit (bit 0) */ for ( int i = BIT_LENGTH - 1; i >= 0; i--) { if (i >= 2) { /* Create a 3-bit window consisting of bits i, i-1, and i-2 */ uint32_t window = (((input_int >> i) & 1) << 2) | (((input_int >> (i - 1)) & 1) << 1) | ((input_int >> (i - 2)) & 1); /* Check if the window matches the pattern '101' */ if (0b101 == window) { /* Replace with '011' and shift the result accordingly */ result = (result << 3) | 0b011; /* Skip the next two bits to avoid overlapping matches */ i -= 2; continue; } } /* If no match is found, simply append the current bit to the result */ result = (result << 1) | ((input_int >> i) & 1); } return result; } /**************************************************************************************************/ /* PUBLIC_FUNCTIONS */ /**************************************************************************************************/ /**************************************************************************************************/ /* GLOBAL_FUNCTIONS */ /**************************************************************************************************/ /* * Function: main * Purpose: Entry point of the program. Demonstrates the use of the pattern_replace function. * Parameters: * input - 32-bit unsigned integer to be processed by pattern_replace function * output - result of pattern replacement applied to input * Return value: * 0 on successful execution, non-zero otherwise. * Notes: * This function is for demonstration purposes only. */ int main() { uint32_t input; /* Input value */ /* Prompt the user to enter a hexadecimal value */ printf("Enter a 32-bit unsigned hexadecimal number (e.g., 0x12345678): "); scanf("%x", &input); /* Process the input using the pattern replacement function */ uint32_t output = pattern_replace(input); /* Output the processed values */ printf("Output: 0x%08x\n", output); return 0; }——现在要写一个程序设计概要,目前已有初稿如下,请你修改润色下,写的更加详细深入:1. 位模式替换 1.1. 前言 1.1.1. 项目简要说明 开发一个函数,实现32位无符号整数中二进制模式"101"到"011"的替换功能。 1.1.2. 任务概述 独立功能模块开发,属于新设计模块。需满足编码规范要求。 1.2. 需求分析 1.2.1. 功能需求 输入:32位无符号整数(十六进制格式) 处理:从高位到低位扫描,识别连续"101"模式 替换:将"101"替换为"011",已替换部分不再参与匹配 输出:替换后的32位整数(十六进制格式) 1.2.2. 约束条件 禁止使用字符串/数组转换 必须使用位操作实现 已替换部分直接跳过 1.3. 原理概述 该程序使用滑动窗口进行模式匹配和替换,,具体流程如下: 创建3位滑动窗口(bit[i], bit[i-1], bit[i-2]) 窗口值计算:window = (bit[i]<<2) | (bit[i-1]<<1) | bit[i-2] 模式匹配:window == 0b101 替换操作:输出0b011并跳过后续2位 非匹配情况:输出当前最高位
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值