横丁の名探偵

トップ > Cマガ電脳クラブ挑戦記録の部屋(過去) > Cマガ電脳クラブソースリスト(1997年分)



第81回(12月)いちいち調べる?

#include <stdio.h>
#include <math.h>
int cnt = 0;
void sub(int a , double r)
{
    int a1 , a2;
    if(r > 0.0) a1 = (int)((double)a * r + 0.5);
    else        a1 = (int)((double)a * r - 0.5);
    a2 = (int)((double)a * r * r + 0.5);
    if(a + a1 + a2 != 111) return;
    if(a * a2 != a1 * a1) return;
    printf("%4d %4d %4d\n" , a , a1 , a2);
    cnt++;
}
int main(void)
{
    int a;
    double s , q;
    for(a = 1 ; a < 149 ; a++) {
        s = 111.0 / (double)a;
        q = 1.0 - 4.0 * (1.0 - s);
        sub(a , (-1.0 + sqrt(q)) / 2.0);
        if(q != 0.0) sub(a , (-1.0 - sqrt(q)) / 2.0);
    }
    printf("%d個\n" , cnt);
    return(0);
}


第79回(10月)Planar Pentacubes

#include <stdio.h>
typedef struct {
    int x;
    int y;
} XY;
typedef struct {
    int x;
    int y;
    int z;
} XYZ;
XY ta[12][5] = {
0,0 , 1,0 , -1,1 , 0,1 , 0,2 ,
0,0 , 0,1 , 0,2 , 0,3 , 0,4 ,
0,0 , 0,1 , 0,2 , 0,3 , 1,3 ,
0,0 , 0,1 , -1,1 , -2,1 , -2,2 ,
0,0 , 1,0 , 0,1 , 1,1 , 0,2 ,
0,0 , 1,0 , 2,0 , 1,1 , 1,2 ,
0,0 , 2,0 , 0,1 , 1,1 , 2,1 ,
0,0 , 0,1 , 0,2 , 1,2 , 2,2 ,
0,0 , 0,1 , 1,1 , 1,2 , 2,2 ,
0,0 , -1,1 , 0,1 , 1,1 , 0,2 ,
0,0 , -1,1 , 0,1 , 0,2 , 0,3 ,
0,0 , 0,1 , -1,1 , -1,2 , -1,3
};
XYZ tb[12][24][5];
int ntb[12];
XYZ tc[24] = {
    0,1,2 , 0,1,-2 , 0,-1,2 , 0,-1,-2 ,
    0,2,1 , 0,2,-1 , 0,-2,1 , 0,-2,-1 ,
    1,0,2 , 1,0,-2 , -1,0,2 , -1,0,-2 ,
    2,0,1 , 2,0,-1 , -2,0,1 , -2,0,-1 ,
    1,2,0 , 1,-2,0 , -1,2,0 , -1,-2,0 ,
    2,1,0 , 2,-1,0 , -2,1,0 , -2,-1,0
};
XYZ td[12][5][12][24][5];
int ntd[12][5][12];
XY aa[5] = {
0,0 , 1,0 , 2,0 , 0,1 , 1,1
};
int cnt = 0;
void disp(int tow[12][2][3])
{
    int i;
    int x , y , z;
    for(z = 0 ; z < 12 ; z++) {
        printf("%2d " , tow[z][0][0]);
        printf("%2d " , tow[z][0][1]);
        printf("%2d\n" , tow[z][0][2]);
        printf("%2d " , tow[z][1][0]);
        printf("%2d\n" , tow[z][1][1]);
    }
}
void sort(int i , int j)
{
    int n1 , n2;
    int x , y , z;
    for(n1 = 0 ; n1 < 5 ; n1++) {
        for(n2 = n1 + 1 ; n2 < 5 ; n2++) {
            if(tb[i][j][n1].z > tb[i][j][n2].z ||
              (tb[i][j][n1].z == tb[i][j][n2].z &&
               tb[i][j][n1].y > tb[i][j][n2].y) ||
              (tb[i][j][n1].z == tb[i][j][n2].z &&
               tb[i][j][n1].y == tb[i][j][n2].y &&
               tb[i][j][n1].x > tb[i][j][n2].x)) {
                x = tb[i][j][n1].x;
                y = tb[i][j][n1].y;
                z = tb[i][j][n1].z;
                tb[i][j][n1].x = tb[i][j][n2].x;
                tb[i][j][n1].y = tb[i][j][n2].y;
                tb[i][j][n1].z = tb[i][j][n2].z;
                tb[i][j][n2].x = x;
                tb[i][j][n2].y = y;
                tb[i][j][n2].z = z;
            }
        }
    }
    x = tb[i][j][0].x;
    y = tb[i][j][0].y;
    z = tb[i][j][0].z;
    for(n1 = 0 ; n1 < 5 ; n1++) {
        tb[i][j][n1].x -= x;
        tb[i][j][n1].y -= y;
        tb[i][j][n1].z -= z;
    }
}
void init1(void)
{
    int n;
    int sw;
    int i0 , i1 , i2 , i3;
    for(i0 = 0 ; i0 < 12 ; i0++) {
        n = 0;
        for(i1 = 0 ; i1 < 24 ; i1++) {
            for(i3 = 0 ; i3 < 5 ; i3++) {
                if     (tc[i1].x ==  0) tb[i0][n][i3].x = 0;
                else if(tc[i1].x ==  1) tb[i0][n][i3].x =   ta[i0][i3].x;
                else if(tc[i1].x == -1) tb[i0][n][i3].x = - ta[i0][i3].x;
                else if(tc[i1].x ==  2) tb[i0][n][i3].x =   ta[i0][i3].y;
                else if(tc[i1].x == -2) tb[i0][n][i3].x = - ta[i0][i3].y;
                if     (tc[i1].y ==  0) tb[i0][n][i3].y = 0;
                else if(tc[i1].y ==  1) tb[i0][n][i3].y =   ta[i0][i3].x;
                else if(tc[i1].y == -1) tb[i0][n][i3].y = - ta[i0][i3].x;
                else if(tc[i1].y ==  2) tb[i0][n][i3].y =   ta[i0][i3].y;
                else if(tc[i1].y == -2) tb[i0][n][i3].y = - ta[i0][i3].y;
                if     (tc[i1].z ==  0) tb[i0][n][i3].z = 0;
                else if(tc[i1].z ==  1) tb[i0][n][i3].z =   ta[i0][i3].x;
                else if(tc[i1].z == -1) tb[i0][n][i3].z = - ta[i0][i3].x;
                else if(tc[i1].z ==  2) tb[i0][n][i3].z =   ta[i0][i3].y;
                else if(tc[i1].z == -2) tb[i0][n][i3].z = - ta[i0][i3].y;
            }
            sort(i0 , n);
            sw = 1;
            for(i2 = 0 ; i2 < n ; i2++) {
                sw = 0;
                for(i3 = 0 ; i3 < 5 ; i3++) {
                    if(tb[i0][i2][i3].x != tb[i0][n][i3].x) sw = 1;
                    if(tb[i0][i2][i3].y != tb[i0][n][i3].y) sw = 1;
                    if(tb[i0][i2][i3].z != tb[i0][n][i3].z) sw = 1;
                }
                if(sw == 0) break;
            }
            if(sw == 1) n++;
        }
        ntb[i0] = n;
    }
}
void init2(void)
{
    int i0 , i1 , i2 , i3;
    int x , y , z;
    int x0 , y0 , z0;
    int n;
    int sw;
    for(z0 = 0 ; z0 < 12 ; z0++) {
        for(i0 = 0 ; i0 < 5 ; i0++) {
            x0 = aa[i0].x;
            y0 = aa[i0].y;
            for(i1 = 0 ; i1 < 12 ; i1++) {
                n = 0;
                for(i2 = 0 ; i2 < ntb[i1] ; i2++) {
                    sw = 0;
                    for(i3 = 0 ; i3 < 5 ; i3++) {
                        y = y0 + tb[i1][i2][i3].y;
                        if(y < 0 || y > 1) {
                            sw = 1;
                            continue;
                        }
                        x = x0 + tb[i1][i2][i3].x;
                        if(x < 0 || (y == 0 && x > 2) || (y == 1 && x > 1)) {
                            sw = 1;
                            continue;
                        }
                        z = z0 + tb[i1][i2][i3].z;
                        if(z < 0 || z > 11) {
                            sw = 1;
                            continue;
                        }
                    }
                    if(sw == 1) continue;
                    for(i3 = 0 ; i3 < 5 ; i3++) {
                        td[z0][i0][i1][n][i3].x = x0 + tb[i1][i2][i3].x;
                        td[z0][i0][i1][n][i3].y = y0 + tb[i1][i2][i3].y;
                        td[z0][i0][i1][n][i3].z = z0 + tb[i1][i2][i3].z;
                    }
                    n++;
                }
                ntd[z0][i0][i1] = n;
            }
        }
    }
}
void sub(int tow[12][2][3] , int rr[12] , int nz , int na)
{
    int x0 , y0;
    int nnz , nna;
    int i0 , i1 , i2;
    int x , y , z;
    int sw;
    nna = na + 1;
    nnz = nz;
    if(nna == 5) {
        nna = 0;
        nnz++;
        if(nnz == 12) {
            cnt++;
            return;
        }
    }
    x0 = aa[na].x;
    y0 = aa[na].y;
    if(tow[nz][y0][x0] != 0) {
        sub(tow , rr , nnz , nna);
        return;
    }
    for(i0 = 0 ; i0 < 12 ; i0++) {
        if(rr[i0] != 0) continue;
        for(i1 = 0 ; i1 < ntd[nz][na][i0] ; i1++) {
            sw = 0;
            for(i2 = 0 ; i2 < 5 ; i2++) {
                x = td[nz][na][i0][i1][i2].x;
                y = td[nz][na][i0][i1][i2].y;
                z = td[nz][na][i0][i1][i2].z;
                if(tow[z][y][x] != 0) {
                    sw = 1;
                    break;
                }
            }
            if(sw == 1) continue;
            for(i2 = 0 ; i2 < 5 ; i2++) {
                x = td[nz][na][i0][i1][i2].x;
                y = td[nz][na][i0][i1][i2].y;
                z = td[nz][na][i0][i1][i2].z;
                tow[z][y][x] = i0 + 1;
            }
            rr[i0] = 1;
            sub(tow , rr , nnz , nna);
            for(i2 = 0 ; i2 < 5 ; i2++) {
                x = td[nz][na][i0][i1][i2].x;
                y = td[nz][na][i0][i1][i2].y;
                z = td[nz][na][i0][i1][i2].z;
                tow[z][y][x] = 0;
            }
            rr[i0] = 0;
        }
    }
}
int main(void)
{
    int x , y , z;
    int tow[12][2][3];
    static int rr[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
    for(z = 0 ; z < 12 ; z++) {
        for(y = 0 ; y < 2 ; y++) {
            for(x = 0 ; x < 3 ; x++) {
                tow[z][y][x] = 0;
            }
        }
    }
    init1();
    init2();
    sub(tow , rr , 0 , 0);
    printf("%d\n" , cnt);
    return(0);
}


第78回(9月)Planar Pentacubes

#include <stdio.h>
#define NN 500
#define B(y , x) *(b + (y) * xsize + x)
static int tt[8][4][2] = {
    0,1 , 1,1 , 2,1 , 2,0 ,
    1,0 , 0,1 , 0,2 , 1,2 ,
    2,1 , 2,0 , 1,0 , 0,1 ,
    0,2 , 1,2 , 1,1 , 1,0 ,
    1,0 , 2,0 , 1,1 , 1,2 ,
    0,1 , 0,2 , -1,1 , -2,1 ,
    1,2 , 0,2 , -1,2 , 0,1 ,
    0,2 , 0,1 , 1,1 , 2,1};
void disp(int *b , int xsize , int ysize)
{
    char p[100][100][2];
    int x , y;
    int cnt = 101;
    int type1;
    int i;
    printf("%d * %d\n" , xsize , ysize);
    for(y = 0 ; y < ysize ; y++) {
        for(x = 0 ; x < xsize ; x++) {
            if(B(y , x) > 0 && B(y , x) < 100) {
                type1 = B(y , x) - 1;
                B(y , x) = cnt;
                for(i = 0 ; i < 4 ; i++) {
                    B(y + tt[type1][i][1] , x + tt[type1][i][0]) = cnt;
                }
                cnt++;
            }
        }
    }
    printf("┌");
    for(x = 0 ; x < xsize ; x++) {
        printf("─");
        if     (x == xsize - 1)           printf("┐");
        else if(B(0 , x) == B(0 , x + 1)) printf("─");
        else                              printf("┬");
    }
    printf("\n");
    for(y = 0 ; y < ysize ; y++) {
        printf("│");
        for(x = 0 ; x < xsize ; x++) {
            printf("  ");
            if     (x == xsize - 1)           printf("│");
            else if(B(y , x) == B(y , x + 1)) printf("  ");
            else                              printf("│");
        }
        printf("\n");
        if(y < ysize - 1) {
            if(B(y , 0) == B(y + 1 , 0)) printf("│");
            else                         printf("├");
            for(x = 0 ; x < xsize ; x++) {
                if(B(y , x) == B(y + 1 , x)) printf("  ");
                else                         printf("─");
                if     (x == xsize - 1) {
                    if(B(y , x) == B(y + 1 , x)) printf("│");
                    else                         printf("┤");
                }
                else if(B(y , x) == B(y , x + 1)) {
                    if     (B(y + 1 , x) == B(y + 1 , x + 1)) printf("─");
                    else if(B(y , x + 1) == B(y + 1 , x)) printf("┌");
                    else if(B(y , x+ 1) == B(y + 1 , x + 1)) printf("┐");
                    else if(B(y , x) != B(y + 1 , x)) printf("┬");
                }
                else if(B(y , x) == B(y + 1 , x)) {
                    if     (B(y , x + 1) == B(y + 1 , x + 1)) printf("│");
                    else if(B(y + 1 , x) == B(y + 1 , x + 1)) printf("└");
                    else if(B(y , x + 1) != B(y + 1 , x + 1)) printf("├");
                }
                else if(B(y , x + 1) == B(y + 1 , x + 1)) {
                    if     (B(y + 1 , x) == B(y + 1 , x + 1)) printf("┘");
                    else if(B(y , x) != B(y + 1 , x)) printf("┤");
                }
                else if(B(y + 1 , x) == B(y + 1 , x + 1))     printf("┴");
                else                    printf("┼");
            }
        }
        else {
            printf("└");
            for(x = 0 ; x < xsize ; x++) {
                printf("─");
                if(x == xsize - 1) printf("┘");
                else if(B(y , x) == B(y , x + 1)) printf("─");
                else printf("┴");
            }
        }
        printf("\n");
    }
}
int check(int *b , int x , int y , int xsize , int ysize , int type)
{
    int i;
    int type1;
    if(B(y , x) != 0) return(0);
    if     (type == 1) {
        if(x + 2 >= xsize) return(0);
        if(y + 1 >= ysize) return(0);
    }
    else if(type == 2) {
        if(x + 1 >= xsize) return(0);
        if(y + 2 >= ysize) return(0);
    }
    else if(type == 3) {
        if(x + 2 >= xsize) return(0);
        if(y + 1 >= ysize) return(0);
    }
    else if(type == 4) {
        if(x + 1 >= xsize) return(0);
        if(y + 2 >= ysize) return(0);
    }
    else if(type == 5) {
        if(x + 2 >= xsize) return(0);
        if(y + 2 >= ysize) return(0);
    }
    else if(type == 6) {
        if(x - 2 < 0) return(0);
        if(y + 2 >= ysize) return(0);
    }
    else if(type == 7) {
        if(x - 1 < 0) return(0);
        if(x + 1 >= xsize) return(0);
        if(y + 2 >= ysize) return(0);
    }
    else if(type == 8) {
        if(x + 2 >= xsize) return(0);
        if(y + 2 >= ysize) return(0);
    }
    type1 = type - 1;
    for(i = 0 ; i < 4 ; i++) {
         if(B(y + tt[type1][i][1] , x + tt[type1][i][0]) != 0) return(0);
    }
    B(y , x) = type;
    for(i = 0 ; i < 4 ; i++) {
         B(y + tt[type1][i][1] , x + tt[type1][i][0]) = -type;
    }
    return(1);
}
void unset(int *b , int x , int y , int xsize , int ysize)
{
    int i;
    int type1;
    type1 = B(y , x) - 1;
    B(y , x) = 0;
    for(i = 0 ; i < 4 ; i++) {
        B(y + tt[type1][i][1] , x + tt[type1][i][0]) = 0;
    }
}
void sub(int *b , int x , int y , int xsize , int ysize)
{
    int type;
    if(x == xsize) {
        x = 0;
        y++;
        if(y == ysize) {
            disp(b , xsize , ysize);
            exit(0);
        }
    }
    if(B(y , x) != 0) {
        sub(b , x + 1 , y , xsize , ysize);
        return;
    }
    for(type = 1 ; type < 9 ; type++) {
        if(check(b , x , y , xsize , ysize , type) == 1) {
            sub(b , x + 1 , y , xsize , ysize);
            unset(b , x , y , xsize , ysize);
        }
    }
}
void submain3(int *b , int xsize , int ysize)
{
    if(check(b , xsize - 1 , ysize - 6 , xsize , ysize , 6) == 1) {
        if(check(b , xsize - 3 , ysize - 4 , xsize , ysize , 2) == 1) {
            if(check(b , xsize - 4 , ysize - 3 , xsize , ysize , 7) == 1) {
                if(check(b , xsize - 2 , ysize - 3 , xsize , ysize , 4) == 1) {
                    sub(b , 2 , 0 , xsize , ysize);
                    unset(b , xsize - 2 , ysize - 3 , xsize , ysize);
                }
                unset(b , xsize - 4 , ysize - 3 , xsize , ysize);
            }
            unset(b , xsize - 3 , ysize - 4 , xsize , ysize);
        }
        unset(b , xsize - 1 , ysize - 6 , xsize , ysize);
    }
    if(check(b , xsize - 1 , ysize - 5 , xsize , ysize , 6) == 1) {
        if(check(b , xsize - 5 , ysize - 3 , xsize , ysize , 7) == 1) {
            if(check(b , xsize - 4 , ysize - 3 , xsize , ysize , 3) == 1) {
                if(check(b , xsize - 3 , ysize - 2 , xsize , ysize , 1) == 1) {
                    sub(b , 2 , 0 , xsize , ysize);
                    unset(b , xsize - 3 , ysize - 2 , xsize , ysize);
                }
                unset(b , xsize - 4 , ysize - 3 , xsize , ysize);
            }
            unset(b , xsize - 5 , ysize - 3 , xsize , ysize);
        }
        unset(b , xsize - 1 , ysize - 5 , xsize , ysize);
    }
}
void submain2(int *b , int xsize , int ysize)
{
    if(check(b , 0 , ysize - 6 , xsize , ysize , 8) == 1) {
        if(check(b , 1 , ysize - 4 , xsize , ysize , 4) == 1) {
            if(check(b , 0 , ysize - 3 , xsize , ysize , 2) == 1) {
                if(check(b , 3 , ysize - 3 , xsize , ysize , 7) == 1) {
                    submain3(b , xsize , ysize);
                    unset(b , 3 , ysize - 3 , xsize , ysize);
                }
                unset(b , 0 , ysize - 3 , xsize , ysize);
            }
            unset(b , 1 , ysize - 4 , xsize , ysize);
        }
        unset(b , 0 , ysize - 6 , xsize , ysize);
    }
    if(check(b , 0 , ysize - 5 , xsize , ysize , 8) == 1) {
        if(check(b , 1 , ysize - 3 , xsize , ysize , 3) == 1) {
            if(check(b , 0 , ysize - 2 , xsize , ysize , 1) == 1) {
                if(check(b , 3 , ysize - 1 , xsize , ysize , 7) == 1) {
                    submain3(b , xsize , ysize);
                    unset(b , 3 , ysize - 1 , xsize , ysize);
                }
                unset(b , 0 , ysize - 2 , xsize , ysize);
            }
            unset(b , 1 , ysize - 3 , xsize , ysize);
        }
        unset(b , 0 , ysize - 5 , xsize , ysize);
    }
}
void submain1(int *b , int xsize , int ysize)
{
    if(check(b , xsize - 6 , 0 , xsize , ysize , 5) == 1) {
        if(check(b , xsize - 3 , 0 , xsize , ysize , 3) == 1) {
            if(check(b , xsize - 4 , 1 , xsize , ysize , 1) == 1) {
                if(check(b , xsize - 1 , 2 , xsize , ysize , 6) == 1) {
                    submain2(b , xsize , ysize);
                    unset(b , xsize - 1 , 2 , xsize , ysize);
                }
                unset(b , xsize - 4 , 1 , xsize , ysize);
            }
            unset(b , xsize - 3 , 0 , xsize , ysize);
        }
        unset(b , xsize - 6 , 0 , xsize , ysize);
    }
    if(check(b , xsize - 5 , 0 , xsize , ysize , 5) == 1) {
        if(check(b , xsize - 2 , 0 , xsize , ysize , 4) == 1) {
            if(check(b , xsize - 2 , 1 , xsize , ysize , 2) == 1) {
                if(check(b , xsize - 1 , 3 , xsize , ysize , 6) == 1) {
                    submain2(b , xsize , ysize);
                    unset(b , xsize - 1 , 3 , xsize , ysize);
                }
                unset(b , xsize - 2 , 1 , xsize , ysize);
            }
            unset(b , xsize - 2 , 0 , xsize , ysize);
        }
        unset(b , xsize - 5 , 0 , xsize , ysize);
    }
}
void submain0(int xsize , int ysize)
{
    int *b;
    int x , y;
    b = (int *)malloc(sizeof(int) * xsize * ysize);
    for(y = 0 ; y < ysize ; y++) {
        for(x = 0 ; x < xsize ; x++) {
            B(y , x) = 0;
        }
    }
    check(b , 0 , 0 , xsize , ysize , 2);
    check(b , 2 , 0 , xsize , ysize , 5);
    check(b , 1 , 1 , xsize , ysize , 4);
    check(b , 0 , 3 , xsize , ysize , 8);
    submain1(b , xsize , ysize);
    unset(b , 0 , 0 , xsize , ysize);
    unset(b , 2 , 0 , xsize , ysize);
    unset(b , 1 , 1 , xsize , ysize);
    unset(b , 0 , 3 , xsize , ysize);
    check(b , 0 , 0 , xsize , ysize , 3);
    check(b , 3 , 0 , xsize , ysize , 5);
    check(b , 1 , 1 , xsize , ysize , 1);
    check(b , 0 , 2 , xsize , ysize , 8);
    submain1(b , xsize , ysize);
    unset(b , 0 , 0 , xsize , ysize);
    unset(b , 3 , 0 , xsize , ysize);
    unset(b , 1 , 1 , xsize , ysize);
    unset(b , 0 , 2 , xsize , ysize);
    free(b);
}
int main(void)
{
    int x , y , xy;
    for(xy = 60 ; xy < NN * 6 ; xy += 5) {
        for(y = 10 ; y <= xy / 6 ; y += 5) {
            if((xy % y) != 0) continue;
            x = xy / y;
            submain0(x , y);
        }
    }
    return(0);
}


第77回(8月)もとどおり数

#include <stdio.h>
#include <math.h>
/* ロング数の定義 /*
typedef struct {
    int n;
    int *m;
} LONG;
/* ロング数をコピーする。 (a = b) /*
void Copy(LONG *a , LONG *b)
{
    int i;
    a->n = b->n;
    a->m = (int *)malloc(a->n * sizeof(int));
    for(i = 0 ; i < a->n ; i++) {
        a->m[i] = b->m[i];
    }
}
/* ロング数を標準出力に出力する。 /*
void Print(LONG a)
{
    int i;
    int sw = 0;
    int d;
    for(i = a.n - 1 ; i >= 0 ; i--) {
        if(sw == 0 && a.m[i] == 0) continue;
        printf(" ");
        d = a.m[i] / 1000;
        if(sw != 0 || d != 0) printf("%1d" , d);
        d = (a.m[i] / 100) % 10;
        if(sw != 0 || d != 0) printf("%1d" , d);
        d = (a.m[i] / 10) % 10;
        if(sw != 0 || d != 0) printf("%1d" , d);
        printf("%1d" , (a.m[i] % 10));
        sw = 1;
    }
    printf("\n");
}
/* ロング数に新規に値をセットする。 (a = b) /*
void New(LONG *a , int b)
{
    int i;
    a->n = 1;
    a->m = (int *)malloc(sizeof(int));
    a->m[0] = b;
}
/* ロング数に数(int)を掛ける。 (a *= b) /*
void Multi(LONG *a , int b)
{
    int i;
    int t = 0;
    for(i = 0 ; i < a->n ; i++) {
        a->m[i] = a->m[i] * b + t;
        t = a->m[i] / 10000;
        a->m[i] %= 10000;
    }
    if(t > 0) {
        (a->n)++;
        a->m = (int *)realloc(a->m , sizeof(int) * a->n);
        a->m[a->n - 1] = t;
    }
}
/* ロング数の各桁の和を求める。 /*
int Get(LONG s)
{
    int i;
    int t = 0;
    for(i = 0 ; i < s.n ; i++) {
        t += (s.m[i] % 10);
        t += (s.m[i] / 10) % 10;
        t += (s.m[i] / 100) % 10;
        t += (s.m[i] / 1000);
    }
    return(t);
}
/* ロング数をクリアする。 /*
void Delete(LONG *s)
{
    s->n = 0;
    free(s->m);
}
/* メイン /*
int main(void)
{
    int q = 2;
    int p , pp , maxp;
    int i;
    int t;
    LONG s , ss;
    int cnt;
    double log10;
    log10 = log(10.0);
    while(1) {
        cnt = 0;
        q++;
        p = 1;
        while(1) {
            p++;
            maxp = ((int)((double)q * log((double)p) / log10) + 1) * 9;
            if(p > maxp) break;
            New(&s , p);
            for(i = 1 ; i < q ; i++) {
                Multi(&s , p);
            }
            t = Get(s);
            if( p == t) {
                pp = p;
                Copy(&ss , &s);
                cnt++;
                if(cnt > 1) break;
            }
            Delete(&s);
        }
        if(cnt > 1) Delete(&ss);
        if(cnt != 1) continue;
        printf("%d ** %d =" , pp , q);
        Print(ss);
        break;
    }
    return(0);
}


第76回(7月)素数魔方陣

#include <stdio.h>
int main(void)
{
    int i0 , i1 , i4;
    int n0 , n1 , n2 , n3 , n4 , n5 , n6 , n7 , n8;
    int s;
    int *t1;
    int *t2;
    int t3[10][3];
    int nt2 = 0 , nt3;
    t1 = (int *)malloc(sizeof(int) * 2);
    if(t1 == (int *)NULL) {
        fprintf(stderr , "malloc error.\n");
        exit(0);
    }
    t2 = (int *)malloc(sizeof(int));
    if(t2 == (int *)NULL) {
        fprintf(stderr , "malloc error.\n");
        exit(0);
    }
    s = 1;
    t1[s] = 1;
    while(1) {
        s++;
        t1 = (int *)realloc(t1 , sizeof(int) * (s + 1));
        if(t2 == (int *)NULL) {
            fprintf(stderr , "realloc error.\n");
            exit(0);
        }
        t1[s] = 0;
        for(i0 = 0 ; i0 < nt2 ; i0++) {
            if((s % t2[i0]) == 0) {
                t1[s] = 1;
                break;
            }
        }
        if(t1[s] == 0) {
            t2 = (int *)realloc(t2 , sizeof(int) * (nt2 + 1));
            if(t2 == (int *)NULL) {
                fprintf(stderr , "realloc error.\n");
                exit(0);
            }
            t2[nt2++] = s;
        }
        if(s < 178) continue;
        nt3 = 0;
        for(i0 = 0 ; i0 < nt2 ; i0++) {
            n0 = t2[i0];
            t1[n0] = 1;
            for(i1 = 0 ; i1 < nt2 ; i1++) {
                n1 = t2[i1];
                if(t1[n1] == 1) continue;
                n2 = s - n0 - n1;
                if(n2 < n0 || t1[n2] == 1) {
                    t1[n1] = 0;
                    continue;
                }
                t1[n2] = 1;
                for(i4 = 0 ; i4 < nt2 ; i4++) {
                    n4 = t2[i4];
                    t1[n4] = 1;
                    n8 = s - n0 - n4;
                    if(n8  < n0 || t1[n8] == 1) {
                        t1[n4] = 0;
                        continue;
                    }
                    t1[n8] = 1;
                    n7 = s - n1 - n4;
                    if(n7 < 2 || t1[n7] == 1) {
                        t1[n4] = t1[n8] = 0;
                        continue;
                    }
                    t1[n7] = 1;
                    n6 = s - n2 - n4;
                    if(n6 < n0 || n2 > n6 || t1[n6] == 1) {
                        t1[n4] = t1[n8] = t1[n7] = 0;
                        continue;
                    }
                    t1[n6] = 1;
                    n3 = s - n0 - n6;
                    if(n3 < 2 || t1[n3] == 1) {
                        t1[n4] = t1[n8] = t1[n7] = t1[n6] = 0;
                        continue;
                    }
                    t1[n3] = 1;
                    n5 = s - n2 - n8;
                    if(n5 < 2 || t1[n5] == 1 || n3 + n4 + n5 != s) {
                        t1[n4] = t1[n8] = t1[n7] = t1[n6] = t1[n3] = 0;
                        continue;
                    }
                    t3[nt3][0] = n0;
                    t3[nt3][1] = n1;
                    t3[nt3][2] = n4;
                    nt3++;
                    t1[n3] = t1[n6] = t1[n7] = t1[n8] = t1[n4] = 0;
                }
                t1[n1] = 0;
            }
            t1[n0] = 0;
        }
        if(nt3 > 1) {
            printf("定和 = %3d\n", s);
            for(i0 = 0 ; i0 < nt3 ; i0++) {
                n0 = t3[i0][0];
                n1 = t3[i0][1];
                n4 = t3[i0][2];
                n2 = s - n0 - n1;
                n8 = s - n0 - n4;
                n7 = s - n1 - n4;
                n6 = s - n2 - n4;
                n3 = s - n0 - n6;
                n5 = s - n2 - n8;
                printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
                    n0,n1,n2,n3,n4,n5,n6,n7,n8);
            }
            return(1);
        }
    }
}


第75回(6月)紅白砂糖

#include <stdio.h>
int main(void)
{
    int x , y , z;
    int s1 , s2;
    int cnt = 0;
    int a1 , a2 , a3;
    for(x = 5 ; x < 23 ; x++) {
        a1 = 4 * x - 8;
        a2 = - a1 * 2;
        a3 = x - 4;
        y = x;
        while(1) {
            s1 = a1 * y + a2;
            s2 = a3 * y - a1;
            if(s2 > 0) {
                z = s1 / s2;
                if(z < y) break;
                if((s1 % s2) == 0) {
                    printf("( %3d , %3d , %3d )\n" , x , y , z);
                    cnt++;
                }
            }
            y++;
        }
    }
    printf("%d\n" , cnt);
    return(0);
}


第73回(4月)詰められ升

#include <stdio.h>
typedef struct {
    int x;
    int y;
    int z;
} XYZ;
void sub(int , int);
void submain(void);
XYZ xyz[64];
int b[4][4][4];
int nn = 0;
void check()
{
    int x , y , z;
    for(z = 0 ; z < 4 ; z++) {
        for(y = 0 ; y < 4 ; y++) {
            for(x = 0 ; x < 4 ; x++) {
                if     (b[x][y][z] == 1) {
                    if(x > 0) {
                        if(b[x - 1][y][z] == 0 &&
                           b[x - 1][y + 1][z] == 0) return;
                    }
                    if(x < 2) {
                        if(b[x + 2][y][z] == 0 &&
                           b[x + 2][y + 1][z] == 0) return;
                    }
                    if(y > 0) {
                        if(b[x][y - 1][z] == 0 &&
                           b[x + 1][y - 1][z] == 0) return;
                    }
                    if(y < 2) {
                        if(b[x][y + 2][z] == 0 &&
                           b[x + 1][y + 2][z] == 0) return;
                    }
                    if(z < 3) {
                        if(b[x][y][z + 1] == 0 &&
                           b[x + 1][y][z + 1] == 0 &&
                           b[x][y + 1][z + 1] == 0 &&
                           b[x + 1][y + 1][z + 1] == 0) return;
                    }
                }
                else if(b[x][y][z] == 2) {
                    if(x > 0) {
                        if(b[x - 1][y][z] == 0 &&
                           b[x - 1][y][z + 1] == 0) return;
                    }
                    if(x < 2) {
                        if(b[x + 2][y][z] == 0 &&
                           b[x + 2][y][z + 1] == 0) return;
                    }
                    if(y > 0) {
                        if(b[x][y - 1][z] == 0 &&
                           b[x + 1][y - 1][z] == 0 &&
                           b[x][y - 1][z + 1] == 0 &&
                           b[x + 1][y - 1][z + 1] == 0) return;
                    }
                    if(y < 3) {
                        if(b[x][y + 1][z] == 0 &&
                           b[x + 1][y + 1][z] == 0 &&
                           b[x][y + 1][z + 1] == 0 &&
                           b[x + 1][y + 1][z + 1] == 0) return;
                    }
                    if(z < 2) {
                        if(b[x][y][z + 2] == 0 &&
                           b[x + 1][y][z + 2] == 0) return;
                    }
                }
                else if(b[x][y][z] == 3) {
                    if(x > 0) {
                        if(b[x - 1][y][z] == 0 &&
                           b[x - 1][y + 1][z] == 0 &&
                           b[x - 1][y + 1][z + 1] == 0 &&
                           b[x - 1][y + 1][z + 1] == 0) return;
                    }
                    if(x < 3) {
                        if(b[x + 1][y][z] == 0 &&
                           b[x + 1][y + 1][z] == 0 &&
                           b[x + 1][y][z + 1] == 0 &&
                           b[x + 1][y + 1][z + 1] == 0) return;
                    }
                    if(y > 0) {
                        if(b[x][y - 1][z] == 0 &&
                           b[x][y - 1][z + 1] == 0) return;
                    }
                    if(y < 2) {
                        if(b[x][y + 2][z] == 0 &&
                           b[x][y + 2][z + 1] == 0) return;
                    }
                    if(z < 2) {
                        if(b[x][y][z + 2] == 0 &&
                           b[x][y + 1][z + 2] == 0) return;
                    }
                }
            }
        }
    }
    printf("%2d個  詰まり升\n" , nn);
    submain();
}
void init()
{
    int n = 0;
    int x , y , z;
    for(z = 0 ; z < 4 ; z++) {
        for(y = 0 ; y < 4 ; y++) {
            for(x = 0 ; x < 4 ; x++) {
                xyz[n].x = x;
                xyz[n].y = y;
                xyz[n].z = z;
                n++;
                b[x][y][z] = 0;
            }
        }
    }
}
int CheckP0(int nXYZ , int nB)
{
    int x , y , z;
    x = xyz[nXYZ].x;
    y = xyz[nXYZ].y;
    z = xyz[nXYZ].z;
/* x - 1 /*
    if(x > 1 && y > 0) {
        if(b[x - 2][y - 1][z] == 1 &&
           b[x    ][y - 1][z] == 0) return(0);
    }
/* x - 2 /*
    if(x > 1 && z > 0) {
        if(b[x - 2][y][z - 1] == 2 &&
           b[x    ][y][z - 1] == 0) return(0);
    }
/* x - 3 /*
    if(x > 0 && y > 0 && z > 0) {
        if(b[x - 1][y - 1][z - 1] == 3 &&
           b[x    ][y - 1][z    ] == 0 &&
           b[x    ][y    ][z - 1] == 0 &&
           b[x    ][y - 1][z - 1] == 0) return(0);
    }
/* y - 1 /*
    if(x > 0 && y > 1) {
        if(b[x - 1][y - 2][z] == 1 &&
           b[x - 1][y    ][z] == 0) return(0);
    }
/* y - 2 /*
    if(x > 0 && y > 0 && z > 0) {
        if(b[x - 1][y - 1][z - 1] == 2 &&
           b[x - 1][y    ][z    ] == 0 &&
           b[x    ][y    ][z - 1] == 0 &&
           b[x - 1][y    ][z - 1] == 0) return(0);
    }
/* y - 3 /*
    if(y > 1 && z > 0) {
        if(b[x][y - 2][z - 1] == 3 &&
           b[x][y    ][z - 1] == 0) return(0);
    }
/* z - 1 /*
    if(x > 0 && y > 0 && z > 0) {
        if(b[x - 1][y - 1][z - 1] == 1 &&
           b[x - 1][y    ][z    ] == 0 &&
           b[x    ][y - 1][z    ] == 0 &&
           b[x - 1][y - 1][z    ] == 0) return(0);
    }
/* z - 2 /*
    if(x > 0 && z > 1) {
        if(b[x - 1][y][z - 2] == 2 &&
           b[x - 1][y][z    ] == 0) return(0);
    }
/* z - 3 /*
    if(y > 0 && z > 1) {
        if(b[x][y - 1][z - 2] == 3 &&
           b[x][y - 1][z    ] == 0) return(0);
    }
/* x + 1 /*
    if(x < 3 && y > 0) {
        if(b[x + 1][y - 1][z] == 1 &&
           b[x    ][y - 1][z] == 0) return(0);
    }
/* x + 2 /*
    if(x < 3 && z > 0) {
        if(b[x + 1][y][z - 1] == 2 &&
           b[x    ][y][z - 1] == 0) return(0);
    }
/* x + 3 /*
    if(x < 3 && y > 0 && z > 0) {
        if(b[x + 1][y - 1][z - 1] == 3 &&
           b[x    ][y - 1][z    ] == 0 &&
           b[x    ][y    ][z - 1] == 0 &&
           b[x    ][y - 1][z - 1] == 0) return(0);
    }
/* y + 2 /*
    if(x > 0 && y < 3 && z > 0) {
        if(b[x - 1][y + 1][z - 1] == 2 &&
           b[x - 1][y    ][z    ] == 0 &&
           b[x    ][y    ][z - 1] == 0 &&
           b[x - 1][y    ][z - 1] == 0) return(0);
    }
/* y + 3 /*
    if(x < 3 && z > 0) {
        if(b[x][y + 1][z - 1] == 3 &&
           b[x][y    ][z - 1] == 0) return(0);
    }
    return(1);
}
int CheckP1(int nXYZ , int nB)
{
    int x , y , z;
    x = xyz[nXYZ].x;
    y = xyz[nXYZ].y;
    z = xyz[nXYZ].z;
    if(x > 2 || y > 2) return(0);
    if(b[x + 1][y    ][z] != 0) return(0);
    if(b[x    ][y + 1][z] != 0) return(0);
    if(b[x + 1][y + 1][z] != 0) return(0);
    if(z > 0) {
        if(b[x    ][y    ][z - 1] == 0 &&
           b[x + 1][y    ][z - 1] == 0 &&
           b[x    ][y + 1][z - 1] == 0 &&
           b[x + 1][y + 1][z - 1] == 0) return(0);
    }
    return(1);
}
int CheckP2(int nXYZ , int nB)
{
    int x , y , z;
    if(nXYZ == 0) return(0);
    x = xyz[nXYZ].x;
    y = xyz[nXYZ].y;
    z = xyz[nXYZ].z;
    if(x > 2 || z > 2) return(0);
    if(y > 0) {
        if(b[x][y - 1][z] == 2) return(0);
    }
    if(b[x + 1][y][z] != 0) return(0);
    if(b[x    ][y][z + 1] != 0) return(0);
    if(b[x + 1][y][z + 1] != 0) return(0);
    if(z > 0) {
        if(b[x][y][z - 1] == 0 && b[x + 1][y][z - 1] == 0) return(0);
    }
    return(1);
}
int CheckP3(int nXYZ , int nB)
{
    int x , y , z;
    if(nXYZ == 0) return(0);
    x = xyz[nXYZ].x;
    y = xyz[nXYZ].y;
    z = xyz[nXYZ].z;
    if(y > 2 || z > 2) return(0);
    if(x > 0) {
        if(b[x - 1][y][z] == 3) return(0);
    }
    if(b[x][y + 1][z    ] != 0) return(0);
    if(b[x][y    ][z + 1] != 0) return(0);
    if(b[x][y + 1][z + 1] != 0) return(0);
    if(z > 0) {
        if(b[x][y][z - 1] == 0 && b[x][y + 1][z - 1] == 0) return(0);
    }
    return(1);
}
void sub(int nXYZ , int nB)
{
    int x , y , z;
    if(nB == nn) {
        check();
        return;
    }
    if(nXYZ == 64) {
        return;
    }
    x = xyz[nXYZ].x;
    y = xyz[nXYZ].y;
    z = xyz[nXYZ].z;
    if(b[x][y][z] != 0) {
        sub(nXYZ + 1 , nB);
        return;
    }
    if(CheckP1(nXYZ , nB) == 1) {
        b[x    ][y    ][z    ] = 1;
        b[x + 1][y    ][z    ] = -1;
        b[x    ][y + 1][z    ] = -1;
        b[x + 1][y + 1][z    ] = -1;
        sub(nXYZ + 1 , nB + 1);
        b[x    ][y    ][z    ] = 0;
        b[x + 1][y    ][z    ] = 0;
        b[x    ][y + 1][z    ] = 0;
        b[x + 1][y + 1][z    ] = 0;
    }
    if(CheckP2(nXYZ , nB) == 1) {
        b[x    ][y    ][z    ] = 2;
        b[x + 1][y    ][z    ] = -1;
        b[x    ][y    ][z + 1] = -1;
        b[x + 1][y    ][z + 1] = -1;
        sub(nXYZ + 1 , nB + 1);
        b[x    ][y    ][z    ] = 0;
        b[x + 1][y    ][z    ] = 0;
        b[x    ][y    ][z + 1] = 0;
        b[x + 1][y    ][z + 1] = 0;
    }
    if(CheckP3(nXYZ , nB) == 1) {
        b[x    ][y    ][z    ] = 3;
        b[x    ][y + 1][z    ] = -1;
        b[x    ][y    ][z + 1] = -1;
        b[x    ][y + 1][z + 1] = -1;
        sub(nXYZ + 1 , nB + 1);
        b[x    ][y    ][z    ] = 0;
        b[x    ][y + 1][z    ] = 0;
        b[x    ][y    ][z + 1] = 0;
        b[x    ][y + 1][z + 1] = 0;
    }
    if(CheckP0(nXYZ , nB) == 1) {
        sub(nXYZ + 1 , nB);
    }
}
void submain()
{
    while(1) {
        nn++;
        if(nn == 16) exit(0);
        init();
        sub(0 , 0);
        printf("%2d個  詰まらない\n",nn);
    }
}
int main(int argc , char **argv)
{
    submain();
    return(0);
}