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