原題下載
答案
(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】的解答,如需了解學校/賽事/課程動態,可至翰林教育官網獲取更多信息。
往期文章閱讀推薦:
2027 USAAIO人工智能奧賽報名進行中!Round 1新規出爐,2月開賽如何備考?
外灘大會見證AI爆發!緊跟智能時代,從USACO計算機奧賽起步!

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