A Different Task

本文详细介绍了汉诺塔问题的解决思路与算法实现,通过计算从初始状态到目标状态所需的最小步骤数,提供了完整的C++代码示例。适用于对递归算法及汉诺塔问题感兴趣的读者。

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

题意:

将汉诺塔从初是状态调到目标状态需要几步

思路:

步数=从初始状态到标准状态+从目标状态到标准状态+1,标准状态是k(当前最大未排好的盘)上的k-1个盘移到中转柱子,+1是移动第k个盘//后来补的的博客,可能有误

/*int f(int n) {
	return 2*f(n-1)+1;
}
基础汉诺塔.
*/
#include<cstdio>
#include<iostream>
#define LL long long;
using namespace std;
int n;
int s[100], t[100];
long long f(int* p, int i, int tran) {
	if(i == 0) return 0;
	if(p[i] == tran) return f(p, i-1, tran);//在中转盘不处理
	long long a = 1LL<<(i-1);
	return f(p, i-1, 6-p[i]-tran) + a;//中转盘变为目标盘,累加的步数基于基础汉诺塔
}
int main() {
	int kase = 0;
	while(scanf("%d", &n) && n) {
		for(int i=1; i<=n; i++) scanf("%d", &s[i]);
		for(int i=1; i<=n; i++) scanf("%d", &t[i]);
		int k=n;
		while(s[k]==t[k] && k) k--;
		if(k != 0) {
			int other = 6 - s[k] - t[k];//移到非起始也非目标的盘上
			printf("Case %d: %lld\n", ++kase, f(s, k-1, other) + f(t, k-1, other) + 1);
		}
		else printf("Case %d: 0\n", ++kase);
	}
	return 0;
}


Inside the OS code, the task scheduler can decide if context switching should be carried out. The operations shown in Figure 10.6 assume that the OS kernel execution is triggered by a SysTick exception, and each time it decides to switch to a different task. If an interrupt request (IRQ) takes place before the SysTick exception, the SysTick exception might preempt the IRQ handler. In this case, the OS should not carry out the context switching. Otherwise, the IRQ handler process will be delayed (Figure 10.7). And for the Cortex -M3 and Cortex-M4 processors, by default the design does not allow return to Thread mode when there is an active interrupt service (but there is an exception e see Non-base Thread Enable description in section 23.5). If the OS attempts to return to the Thread mode with an active interrupt service running, it triggers a Usage fault exception. In some OS designs, this problem is solved by not carrying out context switching if an interrupt service is running. This can easily be done by checking the stacked xPSR from the stack frame, or checking the interrupt active status registers in the NVIC (see section 7.8.4). However, this might affect the performance of the system, especially when an interrupt source keeps generating requests around the SysTick triggering time, which can prevent context switching from happening. The PendSV exception solves the problem by delaying the context-switching request until all other IRQ handlers have completed their processing. To do this, the PendSV is programmed as the lowest-priority exception. If the OS decides that context switching is needed, it sets the pending status of the PendSV, and carries out the context switching within the PendSV exception, as shown in Figure 10.8 show an example sequence of context switching using PendSV with the following event sequence. Figure 10.8 show an example sequence of context switching using PendSV with the following event sequence: 指出要是想进行时间片调度,systick优先级>用户优先级
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值