编程解决智力题之统计正方形

以下为统计正方形个数的程序,图如下:

编程解决智力题之统计正方形_第1张图片

1. 统计顶点

设最左下坐标为(0,0),右上为(4,4), 将所有的顶点(vertex)保存起来,

则大正方形四等分边会获得vertice.size()==25个vertice.

而小正方形二等分边会获得vertice.size()==9个vertice.


2. 统计正方形

然后再判断并统计所有顶点可以组成的正方形个数:

可得:

前者可得 sqares.size()==30 个正方形,其中边长4为1个,边长3为4个,边长2为9个,边长1为16个。

后者可得 sqares.size()==5 个正方形,其中边长2为1个,边长1为4个。

则图中的总个数是 30+2*5=40个。


3. 程序代码:

#include 
#include 
#include 
#include 

struct Vertex
{
    double x;
    double y;
};
typedef struct Vertex Vertex;
struct Square
{
    int a;
    int b;
    int c;
    int d;
};
typedef struct Square Square;
typedef std::vector vec_vtx;
typedef std::vector vec_sqr;
typedef std::vector::iterator vec_sqr_it;

// fill the vertice vector for the sliced Square. 
void fill_vertice(vec_vtx &vertice, int seg)
{
    int v = 0;
    int i,j;
    Vertex unit;
    for (i=0; i<=seg; i++)
        for (j=0; j<=seg; j++)
        {
            unit.x = i;
            unit.y = j;
            vertice.push_back(unit);
        }
}
void illustrate()
{
    // illustrate the graph, * as vertex, --| as bar.
    std::string graph;
    std::string v_bars1 = "|      |    __|__    |      |\n"
        "|      |   |  |  |   |      |\n";
    std::string v_bars2 = "|      |   |__|__|   |      |\n"
        "|      |      |      |      |\n";
    for (int i=0; i<=4; i++)
        for (int j=0; j<=4; j++)
        {
            graph += "*";
            if (j!=4)
                graph += "------";
            if (j==4)
            {
                graph += "\n";
                if (i!=4)
                {
                    if (i%2)
                        graph += v_bars2;
                    else 
                        graph += v_bars1;
                }
            }
        }
    std::cout<<"ahooooo: squares to count:\n"<a;
        b = it->b;
        c = it->c;
        d = it->d;
        std::cout<<"ahooooo: Square: length="<


4. 运行结果


ahooooo: squares to count:
*------*------*------*------*
|      |    __|__    |      |
|      |   |  |  |   |      |
*------*------*------*------*
|      |   |__|__|   |      |
|      |      |      |      |
*------*------*------*------*
|      |    __|__    |      |
|      |   |  |  |   |      |
*------*------*------*------*
|      |   |__|__|   |      |
|      |      |      |      |
*------*------*------*------*

===========華麗麗の割り線===========gorgeous split line======================
ahooooo: Square: length=1 vertex=(0,1,5,6)
ahooooo: Vertex[0]:      (0, 0)
ahooooo: Vertex[1]:      (0, 1)
ahooooo: Vertex[5]:      (1, 0)
ahooooo: Vertex[6]:      (1, 1)
ahooooo: Square: length=2 vertex=(0,2,10,12)
ahooooo: Vertex[0]:      (0, 0)
ahooooo: Vertex[2]:      (0, 2)
ahooooo: Vertex[10]:      (2, 0)
ahooooo: Vertex[12]:      (2, 2)
    ...
ahooooo: Square: length=1 vertex=(18,19,23,24)
ahooooo: Vertex[18]:      (3, 3)
ahooooo: Vertex[19]:      (3, 4)
ahooooo: Vertex[23]:      (4, 3)
ahooooo: Vertex[24]:      (4, 4)
ahooooo: Square count:30
===========華麗麗の割り線===========gorgeous split line======================
ahooooo: Square: length=1 vertex=(0,1,3,4)
ahooooo: Vertex[0]:      (0, 0)
ahooooo: Vertex[1]:      (0, 1)
ahooooo: Vertex[3]:      (1, 0)
ahooooo: Vertex[4]:      (1, 1)
ahooooo: Square: length=2 vertex=(0,2,6,8)
ahooooo: Vertex[0]:      (0, 0)
ahooooo: Vertex[2]:      (0, 2)
ahooooo: Vertex[6]:      (2, 0)
ahooooo: Vertex[8]:      (2, 2)
    ...
ahooooo: Square: length=1 vertex=(4,5,7,8)
ahooooo: Vertex[4]:      (1, 1)
ahooooo: Vertex[5]:      (1, 2)
ahooooo: Vertex[7]:      (2, 1)
ahooooo: Vertex[8]:      (2, 2)

ahooooo: Square count:5
ahooooo: Square total count:40


你可能感兴趣的:(C/C++)