hdu_1030 (找规律)

题意:给你一个数塔,问你两个位置的最短距离。

题解:一般这种规律题都需要对图形特殊处理一下,比如旋转一下,然后就发现规律了。

#include <iostream>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[100020];
int main()
{
	a[1]=1;
	for(ll i=2;i<=100000;i++)
	a[i]=a[i-1]+i*2-1;
	
	ll n,m;
	while(cin>>n>>m)
	{
		ll pos1=lower_bound(a+1,a+99011,n)-a;
		ll low1=(a[pos1]-n+1+1)/2;
		ll up1=(n-a[pos1-1]+1)/2;
			
		ll pos2=lower_bound(a+1,a+99011,m)-a;
		ll low2=(a[pos2]-m+1+1)/2;
		ll up2=(m-a[pos2-1]+1)/2;
		
		ll ans=abs(pos1-pos2);
		ans+=abs(up1-up2);
		ans+=abs(low1-low2);
		
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37632935/article/details/80064564