UVA - 10161 - Ant on a Chessboard

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1102


题意:

     在围棋棋盘上,小虫曲折爬行,如下图。数字表示小虫到达该方格的时间。

     25 24 23 22 21

     10 11 12 13 20

      9  8  7 14 19

      2  3  6 15 18

      1  4  5 16 17


解题:

    其实只是找出数学规律而已。水~

    还特地去翻了等差数列和等比数列的PPT,复习了一下。嗯。都快忘光了~ 推导过程要懂,才不会忘!


#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;

// #define LOCAL_TEST


int main()
{
#ifdef LOCAL_TEST
	freopen("f:\\in.txt", "r", stdin);
	freopen("f:\\out.txt", "w+", stdout);
#endif
	int t;
	int n;
	int re;
	int r, c;
	while ( cin >>t )
	{
		if ( t == 0 )
			break;
		n = sqrt(t * 1.0);
		re = t - n*n;
		if ( re == 0 )
		{
			if (n%2 == 0)
			{
				r = 1;
				c = n;
			}
			else
			{
				r = n;
				c = 1;
			}
		} // end if
		else
		{
			if ( n%2 == 0 )
			{
				if ( re < n+1 )
				{
					c = n+1;
					r = re;
				} // end if
				else
				{
					r = n+1;
					c = 2*n + 2 - re;
				}
			} // end if
			else
			{
				if ( re < n+1 )
				{
					r = n+1;
					c = re;
				} // end if
				else
				{
					c = n+1;
					r = 2*n + 2 - re;
				} // end else
			} // end else
		} // end else
		cout <<c <<' ' <<r <<'\n';
	} // end while
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值