Posted in C/C++,未分类 我抢沙发
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#define INDEX(total, x, y) ((x)*(total*2)+(y))
 
void fill(int *matrix, int total, int value)
{
    if(value < 0)
    {
        matrix[INDEX(total, total, total)] = 0;
        return;
    }
 
    int x_top = total - value;
    int y_left = x_top;
    int x_bottom = 2*total - x_top;
    int y_right = x_bottom;
 
    int x = 0;
    int y = 0;
 
    //(x_top, y_left) ---------> (x_top, y_right)           y changes
    for( x=x_top, y=y_left; y<y_right; y++)
        matrix[INDEX(total, x, y)] = value;
 
    //(x_top,y_right) ---------> (x_bottom, y_right)        x changes
    for( x=x_top, y=y_right; x<x_bottom; x++)
        matrix[INDEX(total, x, y)] = value;
 
    //(x_bottom, y_right) -----> (x_bottom, y_left)         y changes
    for( x=x_bottom, y=y_right; y>y_left; y--)
        matrix[INDEX(total, x, y)] = value;
 
    //(x_bottom, y_left) ------> (x_top, y_left)            x changes
    for( x=x_bottom, y=y_left; x>x_top; x--)
        matrix[INDEX(total, x, y)] = value;
 
    fill(matrix, total, value - 1);
}
 
void fill1(int *matrix, int total, int value)
{
BEGIN:
    if(value < 0)
    {
        matrix[INDEX(total, total, total)] = 0;
        return;
    }
 
    int x_top = total - value;
    int y_left = x_top;
    int x_bottom = 2*total - x_top;
    int y_right = x_bottom;
 
    int x = 0;
    int y = 0;
 
    //(x_top, y_left) ---------> (x_top, y_right)           y changes
    for( x=x_top, y=y_left; y<y_right; y++)
        matrix[INDEX(total, x, y)] = value;
 
    //(x_top,y_right) ---------> (x_bottom, y_right)        x changes
    for( x=x_top, y=y_right; x<x_bottom; x++)
        matrix[INDEX(total, x, y)] = value;
 
    //(x_bottom, y_right) -----> (x_bottom, y_left)         y changes
    for( x=x_bottom, y=y_right; y>y_left; y--)
        matrix[INDEX(total, x, y)] = value;
 
    //(x_bottom, y_left) ------> (x_top, y_left)            x changes
    for( x=x_bottom, y=y_left; x>x_top; x--)
        matrix[INDEX(total, x, y)] = value;
 
    value --;
    goto BEGIN;
}
 
void print(int *matrix, int total)
{
    int x = 0;
    int y = 0;
    int width = 3;
    for(x=0; x<=total*2; x++)
    {
        for(y=0; y<=total*2; y++)
            printf("%d\t", matrix[INDEX(total, x, y)]);
 
        printf("\n");
    }
}
 
int main(int argc, char* argv[])
{
    int total = atoi(argv[1]);
    int *matrix = (int *) malloc ((total*2+1)*(total*2+1)*sizeof(int));
    if(matrix == NULL) 
    {
        printf("error: malloc return NULL\n");
        return 1;
    }
 
    fill1(matrix, total, total);
    print(matrix, total);
 
    free(matrix);
    return 0;
}

从http://www.latelee.org/my-library/156-concentric-matrix.html看到的,写写练练手。
fill是递归的,尾递归,改成fill1,由于不想再格式化代码,用了个goto。如果很反对goto的话,可能看着很难受。
代码中可以用goto,只要不是乱goto就可以。

November 7, 2011