Try   HackMD

C語言 2024年10月APCS實作題第一題詳解(o711)

題目

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

程式碼

int n, w1, w2, h1, h2, i; int max = 0, water, delta; scanf("%d",&n); scanf("%d %d %d %d",&w1,&w2,&h1,&h2); for (i = 1; i <= n; i++) { delta = 0; //每次的倒水,delta都要歸零 scanf("%d",&water); if (water/(w1*w1) <= h1) { //如果 這次倒入的飲料裝在下面的長方體其高度 <= 下方長方體目前剩餘高度(表示下面的就夠裝) delta = water/(w1*w1); //delta表目前的水位高度變化量 h1 -= water/(w1*w1); //h1表下面長方體 "目前剩餘高度" } else { //如果 這次倒入的飲料裝在下面的長方體其高度 > 下方長方體目前剩餘高度(表示下面的不夠裝),則以下分成兩個if: if (h1 > 0) { //下方長方體還有剩餘高度(還可以裝) water -= (w1*w1)*h1; //將下面長方體剩餘的體積全倒入飲料(這時飲料就少了(w1*w1)*h1) delta = h1; h1 -= h1; } if (h1 == 0) { //下方長方體沒有剩餘高度。*因為飲料可能經過在上面的if還沒全倒完,所以這裡的if一定要經過(不能用else if) if (water/(w2*w2) <= h2) { //如果 這次倒入的飲料裝在上面的長方體其高度 <= 下方長方體目前剩餘高度(表示上面的夠裝,不會溢出) delta += water/(w2*w2); h2 -= water/(w2*w2); } else { //如果 這次倒入的飲料裝在上面的長方體其高度 > 下方長方體目前剩餘高度(表示上面的不夠裝,會溢出) delta += h2; h2 -= h2; } } } if (delta > max) { //max表最大水位變化高度。 max = delta; } } printf("%d",max);

執行結果

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →