tower.cpp
来自「本文对递归问题的典型代表汉诺塔问题进行推广——四塔问题。」· C++ 代码 · 共 33 行
CPP
33 行
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
unsigned int n;
long k,j;
int l;
cout<<"input n ( 0 < n < = 50000 )"<<endl<<" n = ";
cin>>n ;
while( n != EOF)
{
if ( n > 50000 || n <= 0 )
{
cout<<"error!\n"<<endl;
return -1;
}
j= ( int ) ( sqrt ( 2.0 * n + 0.25 ) - 0.5 );
if ( j > 3 ) j= ( j - 4 ) % 500 + 4 ;
for ( k = 1, l = 0 ; l < j ; l++ ) k = ( k + k ) % 10000;
j=( ( n -j * ( j - 1 ) / 2 - 1 ) * k + 1 ) %10000;
cout<<"the sum steps is: "<<j<<endl;
cout<<endl;
cout<<"input n ( 0 < n < = 50000 )"<<endl<<" n = ";
cin>>n;
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?