Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1001851
  • 博文数量: 128
  • 博客积分: 10012
  • 博客等级: 上将
  • 技术积分: 2050
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 17:49
文章分类

全部博文(128)

文章存档

2011年(16)

2009年(57)

2008年(55)

分类:

2009-10-21 23:32:56

/* * The Towers Of Hanoi * C * Copyright (C) 1998
Amit Singh. All Rights Reserved.
* * Tested with, ah well ... :) */
#include
#include
#include
#define FROM 1
#define TO 3
#define USING 2
void dohanoi(int N, int from, int to, int using)
{
if (N > 0)
{
 dohanoi(N-1, from, using, to);
 printf ("move %d --> %d\n", from, to);
 dohanoi(N-1, using, to, from);
}
}
int main (int argc, char **argv)
{
long int N;
if (argc != 2)
{
fprintf(stderr, "usage: %s N\n", argv[0]);
exit(1);
}
N = strtol(argv[1], (char **)NULL, 10);
 /* a bit of error checking, LONG_XXX should be there in limits.h */
if (N == LONG_MIN || N == LONG_MAX || N <= 0)
{
fprintf(stderr, "illegal value for number of disks\n");
exit(2);
}
dohanoi(N, FROM, TO, USING); exit(0);
}
阅读(1991) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~