# **UVa 10182 Bee Maja** 今天解了一題UVa的題目 UVa 10125 Sumsets ## 題目敘述如下: ![image](https://hackmd.io/_uploads/HkGNhvwLyx.png) | sample input | sample output| | -------- | -------- | | 1<br>2<br>3<br>4<br>5|0 0<br>0 1<br>-1 1<br>-1 0<br>0 -1| ## 解題心得 這題乍看之下有點複雜,但只要找到規律就解決大部分的問題了。 規律的解讀有很多種,每個人的解讀可能不一樣,實作的方法也就不一樣。 以下是我找到的規律。 首先先觀察資料: <hr> x y 0 0 0 1 -1 1&ensp;x-1,y不變(跟上一個座標差) -1 0&ensp;x不變,y-1 0 -1&ensp;x+1,y-1 1 -1&ensp;x+1,y不變 1 0 &ensp;x不變,y+1 1 1 &ensp;x不變,y+1 0 2 &ensp;x-1,y+1 -1 2 &ensp;x-1,y不變 -2 2 &ensp;x-1,y不變 -2 1 &ensp;x不變,y-1 -2 0 &ensp;x不變,y-1 -1 -1&ensp;x+1,y-1 0 -2&ensp;x+1,y-1 1 -2&ensp;x+1,y不變 2 -2&ensp;x+1,y不變 2 -1&ensp;x不變,y+1 2 0 &ensp;x不變,y+1 2 1 &ensp;x不變,y+1 1 2&ensp;x-1,y+1 0 3 &ensp;x-1,y+1 -1 3 <hr> 可以發現,X與Y之間有規律,正負號間也有規律。 可以將座標分組:只改變x或y與xy皆改變。 接著,用兩個變數,加上if判斷+或-,以及總共需要幾次的+ -。 在進行相對應的動作就可以了 ## 程式碼 ``` #include <iostream> #include <utility> #include <fstream> using namespace std; pair<int, int> coordinate(int space, int time, pair<int, int> ans) { if (space == 1) { } if (space == 2) { ans.second++; } if (space > 2) { ans.first = 0; ans.second = 1; } int xt = 0, yt = 0, c = 0, t = 2; for (int i = 0; i < time; i++) { if (c == t) { for (int j = 0; j < t / 2; j++) { if (t % 2 == 0) // + - { ans.first++; ans.second--; i++; if (i == time) { break; } } else // - + { ans.first--; ans.second++; i++; if (i == time) { break; } } } t++; c = 0; i--; continue; } if (t % 2 == 0) //- { if (c < t / 2) // x { ans.first--; } else // y { ans.second--; } } else // + { if (c <= t / 2 - 1) // x { ans.first++; } else // y { ans.second++; } } c++; } return ans; } int main() { int space = 0; int time; ifstream in; pair<int, int> ans(0, 0); while (cin >> space) { time = space - 2; ans = coordinate(space, time, ans); cout << ans.first << " " << ans.second << endl; } } ``` 以上就是這題的解法,感謝您的瀏覽。
{"title":"UVa 10182 Bee Maja","description":"h2今天解了一題UVa的題目UVa 10125 Sumsets","contributors":"[{\"id\":\"31a4ab52-cc66-4513-91ff-b9050d8d2075\",\"add\":4577,\"del\":1354}]"}
Expand menu