ZOJ1871 POJ2590 Steps

本文分享了一道简单但易错的题目解答过程,通过逐步分析找出规律,确定了在不同步数下行走的最大与最小距离差值。利用C++实现了解题算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很简单的一题,可惜我一开始写错了。。。

最好就是从间隔1开始慢慢找规律,找到n步最长可以走多少,最短可以走多少,就可以了。然后统计对于n步,最长和最短的差值,这就可以找到规律了。



/*******************************************************************************
 * Author : Neo Fung
 * Email : neosfung@gmail.com
 * Last modified : 2011-09-07 16:47
 * Filename : ZOJ1871 POJ2590 Steps.cpp
 * Description : 
 * *****************************************************************************/
// ZOJ1871 POJ2590 Steps.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"



#include <stdio.h>
#include <string.h>

int main(void)
{
	int ncases;
	int a,b,len;
	char str[50];

	while(gets(str) && strlen(str))
	{
		sscanf(str,"%d %d",&a,&b);
		len=b-a;

		int temp=0;
		int index;
		for(int i=0;;++i)
		{
			if(len<=temp+(i+1)/2)
			{
				index=i;
				break;
			}
			temp+=(i+1)/2;
		}
		printf("%d\n",index);
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值