江苏省首届大学生程序设计大赛赛题及解答(一)——Catch That Cow

这是我上周末周日参加程序大赛练习赛的第一题及其我用c++实现的解答。其余的题目我写完后会随后奉上。一些题目不是很难,只有课后习题的难度。不过要在五个小时内写好十题,还是相当有压力的…… 下面的代码经过我的初步检验应该是没什么问题的。至于代码简洁优良与否,望与大家共参之。欢迎大家来提些意见。 第一题还是相当简单的一题,所以我才敢先把这题贴出来。 ------------------------------------------------- Problem A Catch That Cow Input File:catchcow.in Time: 1s Memory: No Limitations Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N(0<=N<=100) on a number line and the cow is at a point K(0<=K<=100) on the same number line. Farmer John has two modes of transportation: walking and teleporting. l Walking: FJ can move from any point X to the points X-1 or X+1 in a single minute l Teleporting: FJ can move from any point X to the point 2*X in a single minute. If the cow ,unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it? Input The input file contains multiple test cases. Every test case have two positive integers: N,K. Proceed to the end of the input file. Output For each case, output the least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow. Please output as the format in the Sample Output. Sample Input 5 16 5 17 Sample Output 3 4 Code list:
// catchthatcow.cpp : Defines the entry point for the console application.
//

#include 
#include 

using namespace std;

int i,N,K,n;
char temp[10],num;

int getway(int K)
{
	if(abs(N-K)>(K/2))
	{
		n++;
		getway(K/2);
	}
	else return n+abs(N-K/2)+1;
}

int main()
{
	ifstream fin("catchcow.in");
	if(!fin)
	{
		cerr << "error: unable to open input file: catchcow.in"<
            
            

            

Shawn Xie

Read more posts by this author.