阿里秋招在线笔试两道编程题
来源:互联网 发布:网络打印机安装失败 编辑:程序博客网 时间:2024/05/18 01:05
2018届秋招,客户端开发工程师笔试的两道编程题
1、饮料兑换活动
阿里巴巴的食堂搞活动促销,已知某饮料1瓶3元钱,4个瓶盖可以换一瓶,2个空瓶可以换一瓶,则30元最多可以喝几瓶。
输入:
A //A表示饮料单价
B //B表示瓶盖换瓶比
C //C表示空瓶换瓶比
D //D表示给定的钱数
输出:S
我觉得关键的是:用瓶盖兑换和空瓶兑换后需要更新总共喝的饮料瓶数、剩余瓶盖数、剩余空瓶数
import java.util.Scanner;public class DrinkActivity { public static void main(String[] args) { Scanner in = new Scanner(System.in); int res; int price; price = Integer.parseInt(in.nextLine().trim()); int cap; cap = Integer.parseInt(in.nextLine().trim()); int emptyBottle; emptyBottle = Integer.parseInt(in.nextLine().trim()); int money; money = Integer.parseInt(in.nextLine().trim()); res = maxMount(price, cap, emptyBottle, money); System.out.println(String.valueOf(res)); } private static int maxMount(int price, int cap, int emptyBottle, int money) { int count = 0; int num_cap = 0; int num_epBottle = 0; if (money < price) { return 0; } count = money / price; num_cap = count; num_epBottle = count; int temp;//记录一轮兑换瓶数 while (num_cap >= cap || num_epBottle >= emptyBottle) { temp = num_cap / cap; num_cap = num_cap % cap; temp = temp + num_epBottle / emptyBottle; num_epBottle = num_epBottle % emptyBottle; count = count + temp; num_cap = num_cap + temp; num_epBottle = num_epBottle + temp; temp = 0; } return count; }}
2、求冰田区域块数
将一个区域,分成 m×n 个块,若某一个块有冰则用 * 表示,无冰则用 0 表示,任给一个这样的区域,求冰田区域(冰田区域划分规则:如果两个小块之间能够在不穿越其他块的情况下连成直线,则**属于同一冰田区块)个数?
这题关键是利用递归调用去遍历属于同一冰田区域的块
import java.util.Scanner;public class KameArea { public static void main(String[] args) { Scanner in = new Scanner(System.in); int m; m = Integer.parseInt(in.nextLine().trim()); String[] sArrays = new String[m]; sArrays[0] = in.nextLine(); int n = sArrays[0].length(); char[][] map = new char[m][n];//存储m*n冰田块信息图 map[0] = sArrays[0].toCharArray(); for (int i = 1; i < m; i++) { sArrays[i] = in.nextLine(); map[i] = sArrays[i].toCharArray(); } boolean[][] isFind = new boolean[m][n];//存储对应map是否遍历 for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) isFind[i][j] = false; } int count = 0;//记录冰田区域数 for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (!isFind[i][j]) { if (map[i][j] == '*') { findMap(map, isFind, i, j, m, n); count++; } else isFind[i][j] = true; } } } System.out.println(String.valueOf(count)); } private static void findMap(char[][] map, boolean[][] isFind, int i, int j, int m, int n) { isFind[i][j] = true; //左上方 if (i > 0 && j > 0 && !isFind[i - 1][j - 1]) { if (map[i - 1][j - 1] == '*') findMap(map, isFind, i - 1, j - 1, m, n); } //上方 if (i > 0 && !isFind[i - 1][j]) { if (map[i - 1][j] == '*') findMap(map, isFind, i - 1, j, m, n); } //右上方 if (i > 0 && j < n - 1 && !isFind[i - 1][j + 1]) { if (map[i - 1][j + 1] == '*') findMap(map, isFind, i - 1, j + 1, m, n); } //左方 if (j > 0 && !isFind[i][j - 1]) { if (map[i][j - 1] == '*') findMap(map, isFind, i, j - 1, m, n); } //右方 if (j < n - 1 && !isFind[i][j + 1]) { if (map[i][j + 1] == '*') findMap(map, isFind, i, j + 1, m, n); } //右下方 if (i < m - 1 && j > 0 && !isFind[i + 1][j - 1]) { if (map[i + 1][j - 1] == '*') findMap(map, isFind, i + 1, j - 1, m, n); } //下方 if (i < m - 1 && !isFind[i + 1][j]) { if (map[i + 1][j] == '*') findMap(map, isFind, i + 1, j, m, n); } //右下方 if (i < m - 1 && j < n - 1 && !isFind[i + 1][j + 1]) { if (map[i + 1][j + 1] == '*') findMap(map, isFind, i + 1, j + 1, m, n); } }}
之前这篇博文写到findMap优化省略几个方向遍历,有点小失误,仔细思考后发现,地图够大且冰块呈锯齿状分布,从中心向周围八个方向遍历都不可省略。
这两道题也只是个人的一些理解,仅供参考。
阅读全文
0 0
- 阿里秋招在线笔试两道编程题
- 2015阿里秋招在线笔试
- 阿里校招在线编程题
- 阿里2018校招笔试编程题
- 阿里秋招编程题
- 2017秋招阿里笔试题--组队
- 2015阿里巴巴秋招在线笔试题
- 阿里2018校招客户端研发笔试之编程题
- 阿里校招笔试题
- 2016恒生电子秋招笔试两道算法题
- 2018阿里秋招在线编程题(Java开发工程师)
- 京东2018校招在线笔试编程题①
- 滴滴2016.09.06校招 在线笔试 - 2道编程题
- 2014 阿里校招研发岗位在线笔试题+附加题
- 2014年8月29日阿里校招在线笔试部分题--软件测试开发
- 2015 阿里校招 Web前端开发 在线笔试总结
- 阿里腾讯2016校招在线笔试有感
- 腾讯2016秋招笔试编程题
- java鬼混笔记:页面验证码生成
- LintCode 372: Delete Node in the Middle of Singly Linked List
- transceiver 接收和发送重配调试
- Memcache遍历查询所有键值的方法
- MySQL数据库安装、配置和运行
- 阿里秋招在线笔试两道编程题
- scapy安装及SCTP包分析
- R词云--wordcloud2
- 【CodeForces 830C】奇怪的降复杂度
- 设计模式原则详解
- 台式GTX1070安装ubuntu16.04和win10双系统
- Codeforces Round #350 (Div. 2) E 链表
- jsp中使用frame
- Oracle使用sqlplus能连接但是SQL Developer连接失败提示IO错误解决方法