poj1543---完美立方(枚举)

#include <stdio.h>

#include <stdlib.h>



int main()

{

    int cube[101],n,i,a,b,c,d;

    for(i=1;i<=100;i++)

    {

        cube[i]=i*i*i;

    }

    scanf("%d",&n);

    for(a=6;a<=n;a++)

    {

        for(b=2;b<a;b++)

        {

            for(c=b+1;c<a;c++)

            {

                for(d=c+1;d<a;d++)

                {

                    if(cube[a]==cube[b]+cube[c]+cube[d])

                        printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d);

                }

            }

        }

    }

    return 0;

}

  

题意:用户输入N,a<=N,使得cube[a]=cube[b]+cube[c]+cube[d],The values of b, c, and d should also be listed in non-decreasing order on the
line itself,同一行的b,c,d,也应该从左至右按照递增的顺序,, one perfect cube per line, in non-decreasing order of a,每一行的a按照递增顺序

输出格式:Cube = 6, Triple = (3,4,5)   分别代表a,b,c,d

思路:从题目case中看出最小a为6,题目, to find integers greater than 1 that satisfy the "perfect cube",所以从b,c,d从取2,3,4开始,个人觉得当a取大值,b,c,d还是从最开始的2,3,4取值,算法效率低,不过这就是枚举

6 2 3 4
6 2 3 5
6 2 4 5
6 3 4 5
7 2 3 4
7 2 3 5
7 2 3 6
7 2 4 5
7 2 4 6
7 2 5 6
7 3 4 5
7 3 4 6
7 3 5 6
7 4 5 6
8 2 3 4

以上为有效进入到if()判断的,没事多写几遍

第一次进入for循环的i的值需要和循环条件比

技巧:cube[20]就表示20的立方,cube[100],需要将数组开成101

你可能感兴趣的:(poj)