ThoughtWorks HomeWork
- The distance of the route A-B-C.
- The distance of the route A-D.
- The distance of the route A-D-C.
- The distance of the route A-E-B-C-D.
- The distance of the route A-E-D.
- The number of trips starting at C and ending at C with a maximum of 3 stops. In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops).
- The number of trips starting at A and ending at C with exactly 4 stops. In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D,E,B).
- The length of the shortest route (in terms of distance to travel) from A to C.
- The length of the shortest route (in terms of distance to travel) from B to B.
- The number of different routes from C to C with a distance of less than 30. In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC.
public class HomeWork {/** * 使用起始站和终点站位节点创建一个二维数组 */ private static int[][] arrs = {{0,5,0,5,7}, {0,0,4,0,0}, {0,0,0,8,2}, {0,0,8,0,6}, {0,3,0,0,0}}; /** * 最短路径 */ private static String shortedPath = ""; /** * 最短路径值 */ private static int shortedVal = Integer.MAX_VALUE; public static void main(String[] args){List<String> list1 = new ArrayList<String>(); list1.add("A"); list1.add("B"); list1.add("C"); String result1 = HomeWork.getRoute(list1);//Test1 System.out.println(result1); List<String> list2 = new ArrayList<String>(); list2.add("A"); list2.add("D"); String result2 = HomeWork.getRoute(list2);//Test2 System.out.println(result2); List<String> list3 = new ArrayList<String>(); list3.add("A"); list3.add("D"); list3.add("C"); String result3 = HomeWork.getRoute(list3);//Test3 System.out.println(result3); List<String> list4 = new ArrayList<String>(); list4.add("A"); list4.add("E"); list4.add("B"); list4.add("C"); list4.add("D"); String result4 = HomeWork.getRoute(list4);//Test4 System.out.println(result4); List<String> list5 = new ArrayList<String>(); list5.add("A"); list5.add("E"); list5.add("D"); String result5 = HomeWork.getRoute(list5);//Test5 System.out.println(result5); List<String> list6 = new ArrayList<>(); List<String> pathList6 = HomeWork.cTocLessThen("C", "C", 3, list6);//Test6 System.out.println(pathList6.size()); int result7 = HomeWork.aTocWith("A", "C", 4);//Test7 System.out.println(result7); int result8 = HomeWork.aTocShorted("A", "C", 0);//Test8 System.out.println(result8); int result9 = HomeWork.aTocShorted("B", "B", 0);//Test9 System.out.println(result9); List<String> list10 = new ArrayList<>(); List<String> pathList10 = HomeWork.cTocValLessThen("C", "C", 0,list10);//Test10 System.out.println(pathList10.size()); }/** * Test1.The distance of the route A-B-C. * Test2.The distance of the route A-D. * Test3.The distance of the route A-D-C. * Test4.The distance of the route A-E-B-C-D. * Test5.The distance of the route A-E-D. */ private static String getRoute(List<String> list){int result = 0; String resultStr = ""; for (int i = 0; i < list.size() - 1; i++){int m = strToInt(list.get(i)); int n = strToInt(list.get(i + 1)); int[] arr = arrs[m]; int path = arr[n]; if (path > 0){result += path; } else {return "NO SUCH ROUTE"; }}if (result > 0){resultStr = result + ""; }else {resultStr = "NO SUCH ROUTE"; }return resultStr; }/** * Test6 The number of trips starting at C and ending at C with a maximum of 3 stops. In the sample data below, there are two such trips: C-D-C (2 stops). and C-E-B-C (3 stops). * @param start 起点 * @param path 路径 * @param maxPath 最大步数 * @param list 路径列表 * @return */ private static List<String> cTocLessThen(String start, String path, int maxPath,List<String> list) {if (path.length() - 1 > maxPath){return list; }if (path.length() > 1 && path.endsWith(start)) { // System.out.println(path); list.add(path); }char lastChar = path.charAt(path.length()-1); int m = lastChar - 'A'; for (int i = 0; i < arrs[m].length; i++){char ch = (char) (i + 'A'); if (arrs[m][i] > 0){cTocLessThen(start,path + ch,maxPath,list); }}return list; }/** * Test7. The number of trips starting at A and ending at C with exactly 4 stops. In the sample data below, there are three such trips: A to C (via B,C,D); A to C (via D,C,D); and A to C (via D,E,B). * @param start 起点 * @param end 终点 * @param step 步数 * @return */ private static int aTocWith(String start,String end,int step){int result = 0; String lastRoute = start; for (int i = 0; i < step; i++){String route = ""; for (int j = 0; j < lastRoute.length(); j++){char ch = lastRoute.charAt(j); int m = ch - 'A'; for (int n = 0; n < arrs[m].length; n++){if (arrs[m][n] > 0){route = route + (char)(n + 'A'); }}}lastRoute = route; }result = lastRoute.split(end).length - 1; return result; }/** * 8. The length of the shortest route (in terms of distance to travel) from A to C. * 9. The length of the shortest route (in terms of distance to travel) from B to B. * @param path 以path为起点的路径 * @param end 终点 * @param pathVal 路径的值 * @return */ private static int aTocShorted(String path,String end,int pathVal){if (path.endsWith(end) && pathVal < shortedVal && pathVal > 0) {shortedPath = path; shortedVal = pathVal; return pathVal; }char lastChar = path.charAt(path.length() - 1); int m = lastChar - 'A'; for (int i = 0; i < arrs[m].length; i++){char ch = (char) (i + 'A'); int value = arrs[m][i]; if (value > 0){if (path.indexOf(ch) > 0){continue; }else {aTocShorted(path + ch,end,pathVal + value); }}}return shortedVal; }/** * 10. The number of different routes from C to C with a distance of less than 30. In the sample data, the trips are: CDC, CEBC, CEBCDC, CDCEBC, CDEBC, CEBCEBC, CEBCEBCEBC. * @param path 路径 * @param end 终点 * @param pathVal 路径值 * @param list 路径列表 * @return */ private static List<String> cTocValLessThen(String path,String end,int pathVal,List<String> list) {if (pathVal >= 30){return list; }if (pathVal > 0 && path.endsWith(end)) { // System.out.println(path); list.add(path); }char lastChar = path.charAt(path.length() - 1); int m = lastChar - 'A'; for (int i = 0; i < arrs[m].length; i++) {char newChar = (char) (i + 'A'); int newCost = arrs[m][i]; if (newCost > 0) {cTocValLessThen(path + newChar, end, pathVal + newCost,list); }}return list; }/** * 将字符串转换成ASCII对应的数字获取二维数组坐标 * @param str * @return */ public static int strToInt(String str){char[] chars = str.toCharArray(); int i = 0; for (char ch : chars){i = ch - 'A'; }return i; } }
ThoughtWorks HomeWork相关推荐
- 记ThoughtWorks面试失败之旅
记ThoughtWorks失败之旅 2012年3月3日面试归来,在一次IT求职交流群讨论的时候,无意说了自己去ThoughtWorks面试失败了,顿时引来大批群众围观,问的问题实在太多了,看来大家都对 ...
- 记Thoughtworks一次糟糕的面试
Boss直聘上投了TW,三月某个周二的下午,TW电话过来.问一下离职原因,我说目前业务方向不对.HR介绍了一个TW,问从哪些途径了解TW等.谈到TW的分享文化,我表示我在团队里面也经常分享.这样,你情 ...
- 聊聊ThoughtWorks面试(郑大版 社招)+ PS:应届生简单流程介绍
聊聊ThoughtWorks面试 Tag: ThoughtWorks 面试 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://dreamhead.blogbus.com ...
- Travel to ThoughtWorks
Life is a journey, not a destination. 人生就是在时光的隧道中不同的奔跑,在不同的环境中遇到不同的人,然后开始相同的挣扎,最后不断优秀的过程.加入ThoughtWo ...
- 思特沃克(ThoughtWorks) 外企面试题 面试经验分析 面试题 软件测试工程师 测试面试题分享 测试需求理解和用例设计编写 SeleniumUI自动化测试 接口测试 性能测试 实战之战损版
文章目录 一.前言 二.面试问题 1.面试题问题汇总 展示 2.Automation Testing Homework V2.pdf 2.1 自动化家庭 作业 2.2 自动化家庭作业 Selenium ...
- thoughtworks 笔试题及答案
我自己做的火车题目答案,供大家参考 火车题目答案:https://download.csdn.net/download/xiananliu/10477156 Hi, It was great spea ...
- 第一次面试——ThoughtWorks
都说 ThoughtWorks 是世界最难面试的 IT 公司之一,不是第一 ,就是第二.所以能去 ThoughtWorks 面试也是一次很不错的体验. 之前一直以为自己面试挂了,总是在默默反思,而从未 ...
- 记ThoughtWorks一次面试经历
最近刚面试完ThoughtWorks android开发工程师,趁此机会记录一下,方面后续回顾. 面试流程 电话面试20分钟 由HR电话沟通一些基本情况, Homework 邮件通知(全英文)有一道a ...
- ThoughtWorks雷达上的新奇变化
与往常一样,ThoughtWorks技术雷达涵盖了四方面内容:语言与框架.平台.技术和工具,而且其中每个领域都会有四方面内容:采用.试用.评估及保留.本文列举了相关领域中较新和值得注意的内容. \\ ...
- comsat java_java-com.thoughtworks.xstream.converters.ConversionExce...
[EDITED] 我正在处理的项目是Java J2EE中的3个文件夹项目,带有servlet和Hibernate以实现持久性.结构如下:-管理员->具有bean和HTML / CSS的主程序-J ...
最新文章
- centos7 安装kubernetes1.4(kubeadm版)
- 全面优化—配置高性能lnmp架构
- vue分页tbale小荔枝
- Module(模块)
- redis 源码 zmalloc.c 实现
- [SPS2010] 使用心得 7 - ebook for Installation
- linux查看用户的操作记录,Linux下查看用户登陆后的操作记录
- java数组元素是类_Java数组及其常用类
- 每个Wi-Fi都有独一无二的IP地址吗?
- [转]linux signal
- 扩展二叉树 (根据特殊的前序遍历建树)
- 基于Android Studio的游戏开发-横版格斗.part
- uni-app 开发微信小程序定位
- 计算机显示器一半有阴影,[显示器图标有影子怎么解决]电脑显示器有字迹影子...
- opengl——贴图
- LDT面试:实验室开发诊断试剂监管模式(Laboratory Developed Test,LDT)
- html5 新增input类型,html5新增的input类型
- AES解密报错,Input length must be multiple of 16 when decrypting with padded cipher
- 国内的IT生意,敢问路在何方?
- 一个老程序员的一些职场经验分享