[藍橋杯2015初賽]移動距離
阿新 • • 發佈:2020-10-13
[藍橋杯2015初賽]移動距離
時間限制:1Sec記憶體限制:256 MB提交:1064解決:463
[狀態] [提交] [命題人:外部匯入]
題目描述
X星球居民小區的樓房全是一樣的,並且按矩陣樣式排列。其樓房的編號為1,2,3...當排滿一行時,從下一行相鄰的樓往反方向排號。
比如:當小區排號寬度為6時,開始情形如下:
1 2 3 4 5 6我們的問題是:已知了兩個樓號m和n,需要求出它們之間的最短移動距離
12 11 10 9 8 7
13 14 15 .....
(不能斜線方向移動)
輸入
輸入存在多組測試資料輸入為3個整數w m n,空格分開,都在1到10000範圍內
w為排號寬度,m,n為待計算的樓號。
輸出
樣例輸入Copy
6 8 2
4 7 20
樣例輸出Copy
4
5
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> using namespace std; int main() { int t,n,m; while(cin>>t>>n>>m) { int a1 = n/t; int b1 = n%t; if(b1 != 0) { //判斷在哪一行,能整除說明在這一行,不能整除就是這一行加1 a1++; } int x1; if(a1%2 == 0) { //偶數行演算法 x1 = a1*t-n+1; } else{ //奇數行演算法 x1 = t-(a1*t-n); } int a2 = m/t; int b2 = m%t; if(b2 != 0) { a2++; } int x2; if(a2%2 == 0) { x2 = a2*t-m+1; } else{ x2 = t-(a2*t-m); } cout<<abs(x1-x2)+abs(a1-a2)<<endl; } return 0; }