记录详情

Accepted

/in/foo.cc: In function 'void move(int, char, char)':
/in/foo.cc:22:21: warning: 'tmp' may be used uninitialized in this function [-Wmaybe-uninitialized]
   case 'C': Cs[Cn++]=tmp;  break;
             ~~~~~~~~^~~~
# 状态 耗时 内存占用
#1 Accepted 1ms 356.0 KiB
#2 Accepted 1ms 400.0 KiB
#3 Accepted 1ms 400.0 KiB
#4 Accepted 1ms 412.0 KiB
#5 Accepted 18ms 424.0 KiB

代码

#include <iostream>
#include <cstdio>
using namespace std;

int N,K, kk;   
int As[30], Bs[30], Cs[30];
int An=0,  Bn=0,  Cn=0;

void move(int n,char from,char to)
{ 
	int tmp,i, Asum=0, Bsum=0, Csum=0;
	switch(from)
	{ 
		case 'A': tmp=As[--An];  break;
		case 'B': tmp=Bs[--Bn];  break;
		case 'C': tmp=Cs[--Cn];  break;
	}
	switch(to)
  	{
		case 'A': As[An++]=tmp;  break;
		case 'B': Bs[Bn++]=tmp;  break;
		case 'C': Cs[Cn++]=tmp;  break;
	}
	for(i=0; i<An; i++) Asum += As[i];
	for(i=0; i<Bn; i++) Bsum += Bs[i];
	for(i=0; i<Cn; i++) Csum += Cs[i];
	kk++;
	if(kk==K)
		printf("%d %d %d\n",Asum,Bsum,Csum);
}

void Hanoi(int n,char from,char help,char to) 
{ 
	if(n==1) { move(n, from, to); return ; }
	Hanoi(n-1,from, to, help);
	move(n, from, to);
	Hanoi(n-1, help, from, to);
}

int main()
{
	cin>>N>>K; An=N;
	As[N-1]=1;   // As[N]示例:{256,128,64,32,16,8,4,2,1}
	for(int i=N-2; i>=0; i--) As[i]=As[i+1]*2;
	kk=0;
	Hanoi(N,'A','B','C'); 
	return 0;
}

信息

递交者
类型
递交
题目
P1017 2-3 Hanoi游戏的状态
语言
C++
递交时间
2021-07-20 11:09:42
评测时间
2021-07-20 11:09:42
评测机
分数
100
总耗时
23ms
峰值内存
424.0 KiB