Codewars : Take a Ten Minute Walk(步行十分钟)

阅读: 评论:0

Codewars : Take a Ten Minute Walk(步行十分钟)

Codewars : Take a Ten Minute Walk(步行十分钟)

纪念第一次刷Codewars

1.问题描述

原题

∙ bullet ∙ You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones – everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. [‘n’, ‘s’, ‘w’, ‘e’]). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don’t want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

∙ bullet ∙ Note: you will always receive a valid array containing a random assortment of direction letters (‘n’, ‘s’, ‘e’, or ‘w’ only). It will never give you an empty array (that’s not a walk, that’s standing still!).


翻译

∙ bullet ∙ 您住在笛卡尔市,那里的所有道路都以完美的网格布局。您提前十分钟到达预约地点,所以您决定趁机散步。该城市在其手机上为市民提供了一个“步行生成”应用程序-每次您按下按钮时,都会向您发送一个由一个字母组成的字符串数组,这些字符串代表行走的方向(例如[‘n’,‘s’,‘w’, ‘e’])。您总是每个字母(方向)只走一个街区,并且知道走过一个城市街区要花费一分钟,因此创建一个函数,如果应用程序给您走,它将返回true,而该函数将花费您正好十分钟(不想早或晚!),当然,它将使您回到起点。否则返回false。

∙ bullet ∙ 注意:您将始终收到一个有效的数组,其中包含方向字母的随机分类(仅’n’,‘s’,‘e’或’w’)。它永远不会给您一个空数组(这不是步行,而是停滞不前!)。

难 度 : 6 k y u {color{Purple} 难度:6kyu} 难度:6kyu


2.问题分析

∙ bullet ∙ 题目重述:给定一个数组,其中有四个方向的走向,当你走十次并刚好走回原点时,返回true,否则返回false。

∙ bullet ∙ 思路:首先判断是否走十分钟。然后分别考虑每个方向,用x来控制左右方向,向左走加1,向右走减1;用y来控制上下方向,向上走加1,向下走减1。初始化 x = 0 , y = 0 , x=0,y=0, x=0,y=0,最后看x和y是否仍然等于0。

∙ bullet ∙ 优化:使用一些特定的函数可以使算法更加的简便。


3.代码

∙ bullet ∙ 普通做法:

#include<vector>
bool isValidWalk(std::vector<char> walk) {if(walk.size() != 10) return false;int x=0, y=0;for(char c : walk){switch(c) {case 'n': y++; break;case 's': y--; break;case 'e': x++; break;case 'w': x--; break;}}if(x == 0 && y == 0) return true;return false;
}

∙ bullet ∙ 使用函数:

#include<vector>
#include <algorithm> //判断条件为是否刚好等于十分钟,并且向左走的步数等于向右走的步数,向上走的步数等于向下走的步数。
bool isValidWalk(std::vector<char> walk) {return walk.size() == 10 && std::count(walk.begin(), d(), 'e') == std::count(walk.begin(), d(), 'w') &&std::count(walk.begin(), d(), 'n') == std::count(walk.begin(), d(), 's');
}

本文发布于:2024-01-28 13:29:56,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064197977748.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:十分钟   Ten   Codewars   Walk   Minute
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23