在C++中判斷是否可以旋轉頁面


在這個問題中,我們得到了頁面上三個點的座標。我們的任務是 *判斷是否可以旋轉頁面*。

 

頁面的旋轉方式是:'x'的新位置是'y'的舊位置,'y'的新位置是'z'的舊位置。根據旋轉結果列印“Yes”或“No”。

讓我們透過一個例子來理解這個問題:

輸入:x = (0, 1), y = (1, 0), z = (0, -1)

輸出:Yes

解釋


我們可以將頁面旋轉90o

解決方案:

如果滿足某些條件,我們就可以旋轉頁面。

如果 *x和y之間的距離* 等於 *y和z之間的距離*,則可以旋轉。此外,如果所有點都在同一條直線上,則無法旋轉。

程式演示了我們解決方案的工作原理:

示例

線上演示

#include<bits/stdc++.h>
using namespace std;

int possibleOrNot(int coordinates[3][2]){
   
   long long dis1 = pow(coordinates[1][0] - coordinates[0][0], 2) + pow(coordinates[1][1] - coordinates[0][1], 2);
   long long dis2 = pow(coordinates[2][0] - coordinates[1][0], 2) + pow(coordinates[2][1] - coordinates[1][1], 2);

   if(dis1 != dis2)
      return 0;
   else if (coordinates[1][0] == ((coordinates[0][0] + coordinates[2][0]) / 2.0) &amp;&amp; coordinates[1][1] == ((coordinates[0][1] + coordinates[2][1]) / 2.0))
      return 0;
   else
      return 1;
}

int main() {
   
   int coordinates[3][2] = {{0 , 1}, {1 , 0}, {0, -1} } ;
   if ( possibleOrNot(coordinates))
      cout<<"The rotation of page is possible";
   else
      cout<<"The rotation of page is not possible";
   
   return 0;
}

輸出

The rotation of page is possible

更新於:2021年1月22日

66 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.