原題下載
答案
(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】的解答,如需了解學校/賽事/課程動態,可至翰林教育官網獲取更多信息。
往期文章閱讀推薦:
耗時一年備考 USACO 卻毫無收獲?一文教你找準最佳備考起步時間!
2026 NOAI國際AI奧賽中國站即將開考!賽事地址&日程已出!

? 2026. All Rights Reserved. 滬ICP備2023009024號-1