日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

天池 在线编程 到达终点

發(fā)布時(shí)間:2024/7/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天池 在线编程 到达终点 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

描述

A robot is located in a pair of integer coordinates (x, y). It must be moved to a location with another set of coordinates. Though the bot can move any number of times, it can only make the following two types of moves:

  • From location (x, y) to location (x + y, y)
  • From location (x, y) to location (x, x + y)

Given the start coordinates and the end coordinates of the target, if the robot can reach the end coordinates according to the given motion rules, you should return true, otherwise you should return false.

x 和 y 的范圍是: [1, 1 000 000 000] 移動(dòng)的次數(shù)可以是0次。 示例 1: 輸入: start = [1, 1] target = [5, 2] 輸出: True 解釋: (1, 1) -> (1, 2) -> (3, 2) -> (5, 2), 你應(yīng)該返回True示例 2: 輸入: start = [1, 2] target = [3, 4] 輸出: False 解釋: [1, 2] 根據(jù)移動(dòng)規(guī)則不能到達(dá) [3, 4] 所以你應(yīng)該返回False.示例 3: 輸入: start = [2, 1] target = [4, 1] 輸出: True 解釋: (2, 1) -> (3, 1) -> (4, 1), 你應(yīng)該返回True.

https://tianchi.aliyun.com/oj/338602522508251883/378934605959598749

2. 解題

  • 從終點(diǎn)往回推,大的坐標(biāo)減去小的坐標(biāo)
class Solution { public:/*** @param start: a point [x, y]* @param target: a point [x, y]* @return: return True and False*/bool ReachingPoints(vector<int> &start, vector<int> &target) {if(start == target) return true;while(target[0] >= start[0] && target[1] >= start[1]){if(start == target)return true;if(target[0] == start[0])//有一個(gè)坐標(biāo)x已經(jīng)相同了{if((target[1]-start[0])%start[0] == 0)return true;//另一個(gè)坐標(biāo) y 一直減 x 即可,是倍數(shù)關(guān)系 trueelsetarget[1] -= start[0];}else if(target[1] == start[1]){if((target[0]-start[1])%start[1] == 0)return true;elsetarget[0] -= start[1];}else if(target[0] > target[1])target[0] -= target[1];elsetarget[1] -= target[0];}return false;} };

我的CSDN博客地址 https://michael.blog.csdn.net/

長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!

總結(jié)

以上是生活随笔為你收集整理的天池 在线编程 到达终点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。