Problem D: Blocks

Problem D: Blocks

时间: 1ms        内存:128M

描述:

Problem D: Blocks

Donald wishes to send a gift to his new nephew, Fooey. Donald is a bit of a traditionalist, so he has chosen to send a set of N classic baby blocks. Each block is a cube, 1 inch by 1 inch by 1 inch. Donald wants to stack the blocks together into a rectangular solid and wrap them all up in brown paper for shipping. How much brown paper does Donald need?

The first line of input contains C, the number of test cases. For each case there is an additional line containing N, the number of blocks to be shipped. N does not exceed 1000. Your program should produce one line of output per case, giving the minimal area of paper (in square inches) needed to wrap the blocks when they are stacked together.

输入:

输出:

示例输入:

5
9
10
26
27
100

示例输出:

30
34
82
54
130

提示:

参考答案(内存最优[752]):

#include <stdio.h>

int C,N,i,j,k,area,best;

main(){
   scanf("%d",&C);
   while (C--) {
      scanf("%d",&N);
      best = 1000*1000*1000;
      for (i=1;i*i*i<=N;i++) {
         for (j=i;i*j*j<=N;j++) {
            if (N%(i*j)) continue;
            k = N/(i*j);
            area = 2*i*j + 2*i*k + 2*j*k;
            if (area < best) best = area;
         }
      }
      printf("%d\n",best);
   }
}

参考答案(时间最优[0]):

#include <stdio.h>

int C,N,i,j,k,area,best;

main(){
   scanf("%d",&C);
   while (C--) {
      scanf("%d",&N);
      best = 1000*1000*1000;
      for (i=1;i*i*i<=N;i++) {
         for (j=i;i*j*j<=N;j++) {
            if (N%(i*j)) continue;
            k = N/(i*j);
            area = 2*i*j + 2*i*k + 2*j*k;
            if (area < best) best = area;
         }
      }
      printf("%d\n",best);
   }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注