阿里笔试 通义实验室笔试 阿里秋招 1012
笔试时间:2025年10月12日
往年笔试合集:
第一题
天才同学在城市上班,每天上下班各一次,一共n天,因此总共有2n次出行,她主要乘坐地铁,且当月累计地铁花费具有分段优惠:
- 默认单次地铁价格为x
- 价格仅由本次乘车前的累计花费cost决定: 若cost ≤ a,单价为x若a < cost ≤ b,单价为y若cost > b,单价为z
- 当月累计地铁花费在单次乘车后首次超过阈值a或b时,从"下一次"乘车起才按更低价格计费
- 其中满足a < b,且票价严格单调递减:x > y > z
她在家里有j把伞,在公司有k把伞。若某次出行遇到下雨:
- 若出发地有伞,则带上一把伞前往目的地,并将伞留在目的地
- 若出发地没有伞,则这次改为打车(不坐地铁,不产生地铁花费)
- 若不下雨,则这次一定乘地铁(不移动伞)
输入描述
输出描述
输出一个整数,表示整个月在地铁上的总花费
样例输入
3 5 10 3 20 1 1 0
010110
样例输出
18
共有3天,6次出行,字符串010110表示的依次为:不下雨、雨、不下雨、雨、雨、不下雨。初始家有1把伞,公司0把。逐次判断:
- 第1次(家→公司,晴):坐地铁,花费5
- 第2次(公司→家,雨):公司无伞,改打车
- 第3次(家→公司,晴):坐地铁,花费5,累计10,刚累计未超10
- 第4次(公司→家,雨):公司无伞,改打车
- 第5次(家→公司,雨):家有伞,带去公司并留在公司,坐地铁,之前累计10,本次价格仍按5计费,乘车后累计为15,之后单次票价变为3
- 第6次(公司→家,晴):坐地铁,当前单次票价为3,最终总花费为5+5+5+3=18
参考题解
解题思路:
- 维护家里和公司的伞数量
- 根据是否下雨和是否有伞决定是否坐地铁
- 累计地铁花费,根据累计值确定单次票价
- 注意票价在超过阈值后的下一次才生效
C++:
#include <iostream> #include <string> using namespace std; int calc_price(int cost, int a, int b, int x, int y, int z) { if (cost <= a) return x; else if (cost <= b) return y; else return z; } pair<bool, pair<int, int>> process_trip(bool morning, bool rain, int home, int comp) { if (!rain) { return {true, {home, comp}}; } if (morning) { if (home > 0) { return {true, {home - 1, comp + 1}}; } else { return {false, {home, comp}}; } } else { if (comp > 0) { return {true, {home + 1, comp - 1}}; } else { return {false, {home, comp}}; } } } int main() { int n, x, a, y, b, z, j, k; cin >> n >> x >> a >> y >> b >> z >> j >> k; string s; cin >> s; int total = 0; int h = j; int c = k; for (int i = 0; i < 2 * n; i++) { bool morning = (i % 2 == 0); bool rain = (s[i] == '1'); auto result = process_trip(morning, rain, h, c); bool take_sub = result.first; h = result.second.first; c = result.second.second; if (take_sub) { int price = calc_price(total, a, b, x, y, z); total += price; } } cout << total << endl; return 0; }
Java:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int x = scanner.nextInt(); int a = scanner.nextInt(); int y = scanner.nextInt(); int b = scanner.nextInt(); int z = scanner.nextInt(); int j = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); int total = 0; int h = j; int c = k; for (int i = 0; i < 2 * n; i++) { boolean morning = (i % 2 == 0); boolean rain = (s.charAt(i) == '1'); Result result = processTrip(morning, rain, h, c); boolean takeSub = result.takeSub; h = result.home; c = result.comp; if (takeSub) { int price = calcPrice(total, a, b, x, y, z); total += price; } } System.out.println(total); } static int calcPrice(int cost, int a, int b, int x, int y, int z) { if (cost <= a) return x; else if (cost <= b) return y; else return z; } static Result processTrip(boolean morning, boolean rain, int home, int comp) { if (!rain) { return new Result(true, home, comp); } if (morning) { if (home > 0) { return new Result(true, home - 1, comp + 1); } else { return new Result(false, home, comp); } } else { if (comp > 0) { return new Result(true, home + 1, comp - 1); } else { return new Result(false, home, comp);
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
2025 春招笔试合集 文章被收录于专栏
2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南