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优化省略几个方向遍历,有点小失误,仔细思考后发现,地图够大且冰块呈锯齿状分布,从中心向周围八个方向遍历都不可省略。
这两道题也只是个人的一些理解,仅供参考。