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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章