原题下载
答案
(Analysis by Nick Wu)
It isn't immediately clear how to fill the larger bucket using the size X bucket and the size Y bucket. Instead of trying to cleverly figure out how many times to use each bucket, we can fix the number of times we use the bucket of size X, and then continually use the bucket of size Y until we would overflow the old bucket. We can loop over all possible numbers for usages of the size X bucket.
Here is my Java code.
import java.io.*;
import java.util.*;
public class pails {
public static void main(String[] args) throws IOException {
// initialize file I/O
BufferedReader br = new BufferedReader(new FileReader("pails.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("pails.out")));
StringTokenizer st = new StringTokenizer(br.readLine());
// read X, Y, and M
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int ans = 0;
// loop over how many times we can pour the X-size bucket
for(int xPour = 0; xPour*x <= m; xPour++) {
// loop over how many times we can then pour over the Y-size bucket
for(int yPour = 0; xPour*x + yPour*y <= m; yPour++) {
// determine if we have filled the bucket more than any previous time
if(xPour*x + yPour*y > ans) {
ans = xPour*x + yPour*y;
}
}
}
// print the answer
pw.println(ans);
// close output stream
pw.close();
}
}
以上就是关于【USACO 2016 February Contest, Bronze Problem 1. Milk Pails】的解答,如需了解学校/赛事/课程动态,可至翰林教育官网获取更多信息。
往期文章阅读推荐:
美国AI奥赛首次新增低龄组!2027 USAAIO报名中,2月正式开考!
不考编程实操?2026 CAICP人工智能能力认证首轮11月22日开考,备考指南速递!

© 2026. All Rights Reserved. 沪ICP备2023009024号-1