【转】http://hi.baidu.com/sunkehappy/blog/item/0440a83e214f143470cf6ccc.html
#include <iostream>
#define DEBUG 1
using namespace std ;
int from, to ;
int stimu( )
{
int i, step = 2 ;
for( i=2,++from,--to; ; ++i ){
from += i ;
++step ;
if( from >= to )
return step ;
to -= i ;
++step ;
if( to <= from )
return step ;
}
}
int main()
{
#if DEBUG
freopen("C:\\Documents and Settings\\Administrator\\桌面\\in.txt","r",stdin) ;
freopen("C:\\Documents and Settings\\Administrator\\桌面\\out.txt","w",stdout) ;
#endif
int i, j ;
while( EOF != scanf("%d%d", &from, &to ) ){
if( to-from <= 2 ){
printf("%d\n", to-from ) ;
continue ;
}
printf("%d\n", stimu( ) ) ;
}
return 0 ;
}
本文展示了一个使用C++编写的程序,该程序通过输入起始和结束位置来计算达到终点所需的最少步数。程序利用了预处理指令、条件判断和循环等基本编程技巧。
785

被折叠的 条评论
为什么被折叠?



