题解:UVA10182 Bee Maja

· · 题解

我晕

看到题目的第一眼,整个人都不好了……

思路:

首先要判断当前是第几圈,然后知道还剩几个蜂巢没有判断,很好写,如下:

int t=0,h=1;
while (n>=h+1) n-=h,t++,h=t*6;

其中 t 表示当前圈数,最后的 n 表示剩余蜂巢个数。

然后,判断此蜂巢在六个边中的第几个边,并且判断坐标。

当它处于第一条边时:(例如 9)
条件:n \le t

x=t-n$,$y=n

当它处于第二条边时:(例如 11)
条件:n \le t \times 2

x=t-n$,$y=t

当它处于第三条边时:(例如 13)
条件:n \le t \times 3

x=-t$,$y=t \times 3-n

当它处于第四条边时:(例如 15)
条件:n \le t \times 4

x=n-t \times 4$,$y=t \times 3-n

当它处于第五条边时:(例如 17)
条件:n \le t \times 5

x=n-t \times 4$,$y=-t

当它处于第六条边时:(例如 19)
条件:n \le t \times 6

x=t$,$y=n-t \times 6

最重要的是:特判特判特判特判特判哇~~~

当输入 1 时要特判!!!

最后,奉上本人新鲜的代码~

#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
    while (cin >> n) //cin是有返回值的,成功输入返回一
    {
        if (n==1)
        {
            cout << "0 0\n";
            continue;
        }//特判!你把我害得好苦~
        int t=0,h=1;
        while (n>=h+1) n-=h,t++,h=t*6;
        if (n<=t) cout << t-n << " " << n << endl;//第一条边
        else if (n<=t*2) cout << t-n << " " << t << endl;//第二条边
        else if (n<=t*3) cout << -t << " " << t*3-n << endl;//第三条边
        else if (n<=t*4) cout << n-t*4 << " " << t*3-n << endl;//第四条边
        else if (n<=t*5) cout << n-t*4 << " " << -t << endl;//第五条边
        else cout << t << " " << n-t*6 << endl;//第六条边
    }
    return 0;//好习惯,益终生
}

这是蒟蒻第一篇题解,见谅见谅!
看完记得点赞!