横丁の名探偵

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



第57回(12月)展開図

/******************************************************************************
 File        : puzzle.c
 Project     : 1995 9 電脳クラブ
 Language    : C
 Date        : 1995.08.18
*****************************************************************************/*
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define ON  1
#define OFF 0
typedef struct {
    int x;
    int y;
} DICE;
void Disp(DICE Dice[])
{
    int i;
    int x , y;
    int fCheck;
    for(y = 0 ; y < 6 ; y++) {
        for(x = 0 ; x < 6 ; x++) {
            fCheck = OFF;
            for(i = 0 ; i < 6 ;i++) {
                if(x == Dice[i].x && y == Dice[i].y) {
                    fCheck = ON;
                }
            }
            if(fCheck == OFF) printf("  ");
            else              printf("□");
        }
        printf("\n");
    }
}
int CheckEqual(int Dice1[6] , int Dice2[6])
{
    int i;
    for(i = 0 ; i < 6 ; i++) {
        if(Dice1[i] != Dice2[i]) return(1);
    }
    return(0);
}
int Cmp(void *a , void *b)
{
    if     (*(int *)a < *(int *)b) return(-1);
    else if(*(int *)a > *(int *)b) return(1);
    else                           return(0);

}
void MakeMirror(DICE Dice1[] , int Dice2[] , int mode)
{
    int i;
    int mx = -1 , my = -1;
    for(i = 0 ; i < 6 ; i++) {
        if(Dice1[i].x > mx) mx = Dice1[i].x;
        if(Dice1[i].y > my) my = Dice1[i].y;
    }
    for(i = 0 ; i < 6 ; i++) {
        if     (mode == 0) Dice2[i] = Dice1[i].x * 6 + (my - Dice1[i].y);
        else if(mode == 1) Dice2[i] = (mx - Dice1[i].x) * 6 + Dice1[i].y;
        else if(mode == 2) Dice2[i] = (mx - Dice1[i].x) * 6 + (my - Dice1[i].y);
        else if(mode == 3) Dice2[i] = Dice1[i].y * 6 + Dice1[i].x;
        else if(mode == 4) Dice2[i] = Dice1[i].y * 6 + (mx - Dice1[i].x);
        else if(mode == 5) Dice2[i] = (my - Dice1[i].y) * 6 + Dice1[i].x;
        else if(mode == 6) Dice2[i] = (my - Dice1[i].y) * 6 + (mx - Dice1[i].x);
    }
    qsort((void  *)Dice2 , (size_t)6 , sizeof(int) , Cmp);
}
int CheckSeq(DICE Dice[])
{
    int i , j , k;
    int fCheck[6];
    if(Dice[0].y > 0 && Dice[1].y > 0 && Dice[2].y > 0 && Dice[3].y > 0 &&
       Dice[4].y > 0 && Dice[5].y > 0) return(0);
    fCheck[0] = ON;
    for(i = 1 ; i < 6 ; i++) fCheck[i] = OFF;
    for(i = 0 ; i < 5 ; i++) {
        for(j = 0 ; j < 6 ; j++) {
            if(fCheck[j] == OFF) continue;
            for(k = 0 ; k < 6 ; k++) {
                if(j == k) continue;
                if((Dice[j].x == Dice[k].x && abs(Dice[j].y - Dice[k].y) == 1)||
                   (Dice[j].y == Dice[k].y && abs(Dice[j].x - Dice[k].x) == 1)){
                    if     (fCheck[j] == ON) fCheck[k] = ON;
                    else if(fCheck[k] == ON) fCheck[j] = ON;
                }
            }
        }
        if(fCheck[0] == ON && fCheck[1] == ON && fCheck[2] == ON &&
           fCheck[3] == ON && fCheck[4] == ON && fCheck[5] == ON) break;
    }
    for(i = 0 ; i < 6 ; i++) {
        if(fCheck[i] == OFF) return(0);
    }
    return(1);
}
int CheckMirror(DICE Dice[] , int grDice[][6] , int n)
{
    int i , j;
    int TmpDice[6];
    for(i = 0 ; i < n ; i++) {
        for(j = 0 ; j < 7 ; j++) {
            MakeMirror(Dice , TmpDice , j);
            if(CheckEqual(grDice[i] , TmpDice) == 0) {
                return(0);
            }
        }
    }
    return(1);
}
int CheckBuild(DICE Dice[])
{
    int wp[6] , wq[6];
    int i , j;
    int dx , dy;
    int h;
    static int tp[6][4] = {1,2,3,4,5,2,0,4,1,5,3,0,0,2,5,4,1,0,3,5,1,4,3,2};
    static int tq[6][4] = {0,0,0,0,2,3,0,1,1,0,3,0,0,1,2,3,3,0,1,0,2,0,2,0};
    int fCheck[6];
    for(i = 0 ; i < 6 ; i++) fCheck[i] = OFF;
    for(i = 0 ; i < 6 ; i++) wp[i] = -1;
    wp[0] = 0;
    wq[0] = 0;
    while(1) {
        for(i = 0 ; i < 6 ; i++) {
            if(wp[i] == -1) continue;
            for(j = 0 ; j < 6 ; j++) {
                if(i == j) continue;
                if(wp[j] != -1) continue;
                dx = Dice[j].x - Dice[i].x;
                dy = Dice[j].y - Dice[i].y;
                if     (dx ==  1 && dy ==  0) h = 0;
                else if(dx ==  0 && dy ==  1) h = 3;
                else if(dx == -1 && dy ==  0) h = 2;
                else if(dx ==  0 && dy == -1) h = 1;
                else continue;
                wp[j] = tp[wp[i]][(h - wq[i] + 8) % 4];
                wq[j] = (tq[wp[i]][(h - wq[i] + 8) % 4] + wq[i]) % 4;
            }
        }
        if(wp[0] > -1 && wp[1] > -1 && wp[2] > -1 && wp[3] > -1 &&
           wp[4] > -1 && wp[5] > -1) break;
    }
    for(i = 0 ; i < 6 ; i++) {
        if(fCheck[wp[i]] == ON) return(0);
        fCheck[wp[i]] = ON;
    }
    return(1);
}
int main(void)
{
    int i0 , i1 , i2 , i3 , i4 , i5;
    DICE Dice[6];
    int grDice[20][6];
    int n = 0;
    for(i0 = 0 ; i0 < 5 ; i0++) {
        Dice[0].x = i0 / 6;
        Dice[0].y = i0 % 6;
        for(i1 = i0 + 1 ; i1 < 20 ; i1++) {
            Dice[1].x = i1 / 6;
            Dice[1].y = i1 % 6;
            if(Dice[1].x + Dice[1].y > 5) continue;
            for(i2 = i1 + 1 ; i2 < 21 ; i2++) {
                Dice[2].x = i2 / 6;
                Dice[2].y = i2 % 6;
                if(Dice[2].x + Dice[2].y > 5) continue;
                for(i3 = i2 + 1 ; i3 < 25 ; i3++) {
                    Dice[3].x = i3 / 6;
                    Dice[3].y = i3 % 6;
                    if(Dice[3].x + Dice[3].y > 5) continue;
                    for(i4 = i3 + 1 ; i4 < 26 ; i4++) {
                        Dice[4].x = i4 / 6;
                        Dice[4].y = i4 % 6;
                        if(Dice[4].x + Dice[4].y > 5) continue;
                        for(i5 = i4 + 1 ; i5 < 31 ; i5++) {
                            Dice[5].x = i5 / 6;
                            Dice[5].y = i5 % 6;
                            if(Dice[5].x + Dice[5].y > 5) continue;
                            if(CheckSeq(Dice) == 0) continue;
                            if(CheckMirror(Dice , grDice , n) == 0) continue;
                            if(CheckBuild(Dice) == 0) continue;
                            grDice[n][0] = i0;
                            grDice[n][1] = i1;
                            grDice[n][2] = i2;
                            grDice[n][3] = i3;
                            grDice[n][4] = i4;
                            grDice[n][5] = i5;
                            n++;
                            Disp(Dice);
                        }
                    }
                }
            }
        }
    }
    printf("解答=%d\n" , n);
    return(0);
}


第56回(11月)加算連鎖

/******************************************************************************
 File        : puzzle.c
 Project     : 1995 8 電脳クラブ
 Language    : C
 Date        : 1995.07.18
*****************************************************************************/*
#include <stdio.h>
#define ON  1
#define OFF 0
int rgKasan[11];                 /* 加算値のテーブル /*
int rgCheck[1025];               /* チェックテーブル /*
int rgIndex1[55] , rgIndex2[55]; /* インデックステーブル /*
void sub(int min , int cnt)
{
    int i;
    if(cnt >= 10) {
        return;
    }
    cnt++;
    for(i = min ; i < 55 ; i++) {
        if(rgIndex1[i] >= cnt) break;
        rgKasan[cnt] = rgKasan[rgIndex1[i]] + rgKasan[rgIndex2[i]];
        if(rgKasan[cnt] <= rgKasan[cnt - 1]) continue;
        rgCheck[rgKasan[cnt]] = ON;
        sub(i + 1 , cnt);
    }
}
int main(void)
{
    int i , j , k;
    for(i = 1 ; i < 1025 ; i++) rgCheck[i] = OFF;
    i = j = 0;
    for(k = 0 ; k < 55 ; k++) {
        rgIndex1[k] = i;
        rgIndex2[k] = j;
        if(i == j) {
            j = 0;
            i++;
        }
        else {
            j++;
        }
    }
    rgKasan[0] = 1;
    rgKasan[1] = rgKasan[0] + rgKasan[0];
    rgCheck[rgKasan[0]] = rgCheck[rgKasan[1]] = ON;
    sub(1 , 1);
    for(i = 1 ; i < 1025 ; i++) {
        if(rgCheck[i] == OFF) {
            printf("%d\n" , i);
            return(1);
        }
    }
    return(0);
}


第55回(10月)カウント・ワン

/******************************************************************************
 1995 7
*****************************************************************************/*
#include <stdio.h>
int main(void)
{
    long   i0 , i1 , i2 , i3 , i4 , i5 , i6 , i7 , i8 , i9;
    double t0 , t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9;
    long        s1 , s2 , s3 , s4 , s5 , s6 , s7 , s8 , s9;
    double cnt = 0.0;
    double dd;
    int k = 0;
    for(i9 = 0L ; i9 < 10L ; i9++) {
      t9 = (double)i9 * 1000000000.0;
      s9 = (i9 == 1L);
      dd = (double)s9 * 1000000000.0 + 900000000.0;
      if(cnt + dd < t9 || t9 + 999999999.0 < cnt) {
        cnt += dd;
        continue;
      }
      for(i8 = 0L ; i8 < 10L ; i8++) {
        t8 = t9 + (double)i8 * 100000000.0;
        s8 = s9 + (i8 == 1L);
        dd = (double)(s8 * 100000000L + 80000000L);
        if(cnt + dd < t8 || t8 + 99999999.0 < cnt) {
          cnt += dd;
          continue;
        }
        for(i7 = 0L ; i7 < 10L ; i7++) {
          t7 = t8 + (double)i7 * 10000000.0;
          s7 = s8 + (i7 == 1L);
          dd = (double)(s7 * 10000000L + 7000000L);
          if(cnt + dd < t7 || t7 + 9999999.0 < cnt) {
            cnt += dd;
            continue;
          }
          for(i6 = 0L ; i6 < 10L ; i6++) {
            t6 = t7 + (double)i6 * 1000000.0;
            s6 = s7 + (i6 == 1L);
            dd = (double)(s6 * 1000000L + 600000L);
            if(cnt + dd < t6 || t6 + 999999.0 < cnt) {
              cnt += dd;
              continue;
            }
            for(i5 = 0L ; i5 < 10L ; i5++) {
              t5 = t6 + (double)i5 * 100000.0;
              s5 = s6 + (i5 == 1L);
              dd = (double)(s5 * 100000L + 50000L);
              if(cnt + dd < t5 || t5 + 99999.0 < cnt) {
                cnt += dd;
                continue;
              }
              for(i4 = 0L ; i4 < 10L ; i4++) {
                t4 = t5 + (double)i4 * 10000.0;
                s4 = s5 + (i4 == 1L);
                dd = (double)(s4 * 10000L + 4000L);
                if(cnt + dd < t4 || t4 + 9999.0 < cnt) {
                  cnt += dd;
                  continue;
                }
                for(i3 = 0L ; i3 < 10L ; i3++) {
                  t3 = t4 + (double)i3 * 1000.0;
                  s3 = s4 + (i3 == 1L);
                  dd = (double)(s3 * 1000L + 300L);
                  if(cnt + dd < t3 || t3 + 999.0 < cnt) {
                    cnt += dd;
                    continue;
                  }
                  for(i2 = 0L ; i2 < 10L ; i2++) {
                    t2 = t3 + (double)i2 * 100.0;
                    s2 = s3 + (i2 == 1L);
                    dd = (double)(s2 * 100L + 20L);
                    if(cnt + dd < t2 || t2 + 99.0 < cnt) {
                      cnt += dd;
                      continue;
                    }
                    for(i1 = 0L ; i1 < 10L ; i1++) {
                      t1 = t2 + (double)i1 * 10.0;
                      s1 = s2 + (i1 == 1L);
                      dd = (double)(s1 * 10L + 1L);
                      if(cnt + dd < t1 || t1 + 9.0 < cnt) {
                        cnt += dd;
                        continue;
                      }
                      for(i0 = 0L ; i0 < 10L ; i0++) {
                        t0 = t1 + (double)i0;
                        cnt += (double)(s1 + (i0 == 1L));
                        if(t0 > 0.0 && t0 == cnt) {
                            k++;
                            printf("%3d %12.0lf\n" , k , t0);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return(0);
}

/******************************************************************************
 1995 7
*****************************************************************************/*
#include <stdio.h>
double cnt = 0.0;
double ma[10];
long mb[10] , mc[10];
void sub0(int n , long i2 , double t2  , long s2)
{
    double t1;
    double dd;
    static int k = 0;
    t1 = t2 + (double)i2 * ma[0];
    cnt += (double)(s2 + (i2 == 1L));
    if(t1 > 0.0 && t1 == cnt) {
        k++;
        printf("%3d %12.0lf\n" , k , t1);
    }
}
void sub1(int n , long i2 , double t2 , long s2)
{
    long i1;
    long s1;
    double t1;
    double dd;
    t1 = t2 + (double)i2 * ma[n];
    s1 = s2 + (i2 == 1L);
    dd = (double)(s1 * mb[n] + mc[n]);
    if(cnt + dd < t1 || t1 + (ma[n] - 1) < cnt) {
        cnt += dd;
        return;
    }
    for(i1 = 0L ; i1 < 10L ; i1++) {
        if     (n == 1) sub0(n - 1 , i1 , t1 , s1);
        else            sub1(n - 1 , i1 , t1 , s1);
    }
}
int main(void)
{
    long i1;
    double dd;
    ma[0] = 1.0;
    mb[0] = 1L;
    mc[0] = 1L;
    for(i1 = 1 ; i1 < 10 ; i1++) {
        ma[i1] = ma[i1 - 1] * 10.0;
        mb[i1] = mb[i1 - 1] * 10L;
        mc[i1] = mb[i1 - 1] * i1;
    }
    for(i1 = 0L ; i1 < 10L ; i1++) {
        sub1(9 , i1 , 0.0 , 0L);
    }
    return(0);
}


第53回(8月)平方数の逆襲

#include <stdio.h>
#include <math.h>
int n1 , n2;
double *nokori;
void sub(double b , int n , int f , int m , int c[])
{
    int i;
    int status;
    if(f == 0) {
        c[n] = 0;
    }
    else {
        c[n] = 1;
        b += 1.0 / (double)(n * n);
        m++;
        if     (b > 0.500000000001) {
            return;
        }
        else if(b > 0.499999999999) {
            for(i = 0 ; i < n + 1 ; i++) {
                if(c[i] == 1) printf("%2d " , i);
            }
            printf("\n");
        }
    }
    if(b + nokori[n] < 0.499999999999) return;
    if(n == n1 - 1) return;
    if(m == n2) return;
    sub(b , n + 1 , 1 , m , c);
    sub(b , n + 1 , 0 , m , c);
}
int main(int argc , char **argv)
{
    int *c;
    int i;
    if(argc != 3) {
        fprintf(stderr , "使用方法 : %s 調べる最大数 個数\n" , argv[0]);
        exit(0);
    }
    n1 = atoi(argv[1]);
    n2 = atoi(argv[2]);
    c = (int *)malloc(sizeof(int) * (n1 + 1));
    if(c == (int *)NULL) {
        exit(0);
    }
    nokori = (double *)malloc(sizeof(double) * (n1 + 1));
    if(nokori == (double *)NULL) {
        free(c);
        exit(0);
    }
    nokori[n1 - 1] = 0.0;
    for(i = n1 - 2 ; i >= 1 ; i--) {
        nokori[i] = 1.0 / (double)((i + 1) * (i + 1)) + nokori[i + 1];
    }
    c[0] = c[1] = 0;
    sub(0.0 , 2 , 1 , 0 , c);
    sub(0.0 , 2 , 0 , 0 , c);
    free(nokori);
    free(c);
    return(0);
}

#include <stdio.h>
#define N 20
typedef struct {
    int n;
    long si[N];
    long bo[N];
} BUNSU;
void set0(BUNSU *bb)
{
    bb->si[0] = 0;
    bb->bo[0] = 1;
    bb->n = 1;
}
void copy(BUNSU *b1 , BUNSU *b2)
{
    int i;
    for(i = 0 ; i < b2->n ; i++) {
        b1->si[i] = b2->si[i];
        b1->bo[i] = b2->bo[i];
    }
    b1->n = b2->n;
}
void tasu(BUNSU *bb , int n)
{
    int i;
    BUNSU tt;
    int sw = 0;
    set0(&tt);
    tt.si[bb->n] = tt.bo[bb->n] = 0;
    for(i = 0 ; i < bb->n ; i++) {
        tt.si[i] = bb->si[i] * n + bb->bo[i];
        tt.bo[i] = bb->bo[i] * n;
    }
    tt.n = bb->n;
    for(i = 0 ; i < tt.n ; i++) {
        if(tt.si[i] >= 10000L) {
            if(i == bb->n - 1) tt.si[i + 1] =  tt.si[i] / 10000L;
            else               tt.si[i + 1] += tt.si[i] / 10000L;
            tt.si[i] %= 10000L;
            if(i == tt.n - 1) sw=1;
        }
        if(tt.bo[i] >= 10000L) {
            if(i == bb->n - 1) tt.bo[i + 1] =  tt.bo[i] / 10000L;
            else               tt.bo[i + 1] += tt.bo[i] / 10000L;
            tt.bo[i] %= 10000L;
            if(i == tt.n - 1) sw=1;
        }
    }
    tt.n += sw;
    copy(bb , &tt);
}
int hikaku(BUNSU *bb)
{
    int si[N];
    int i;
    for(i = 0 ; i < bb->n ; i++) {
        si[i] = bb->si[i] * 2;
    }
    for(i = 0 ; i < bb->n ; i++) {
        if(si[i] >= 10000L) {
            if(i == bb->n - 1) si[i + 1] =  si[i] / 10000L;
            else               si[i + 1] += si[i] / 10000L;
            si[i] %= 10000L;
        }
    }
    for(i = 0 ; i < bb->n ; i++) {
        if(bb->bo[i] != si[i]) return(0);
    }
    return(1);
}
int main(int argc , char **argv)
{
    BUNSU bb;
    int i;
    int n;
    int nn[100];
    int status;
    n = argc - 1;
    for(i = 0 ; i < n ; i++) {
        nn[i] = atoi(argv[i + 1]);
    }
    set0(&bb);
    for(i = 0 ; i < n ; i++) {
        tasu(&bb , nn[i] * nn[i]);
    }
    status = hikaku(&bb);
    if(status == 1) printf("Total is 1/2.\n");
    else            printf("Total is not 1/2.\n");
}
#include <stdio.h>
int main(int argc , char **argv)
{
    int nn;
    int i , j , n;
    int bunsi = 0 , bunbo = 1;
    int b1 , b2;
    int sw;
    n = argc - 1;
    for(i = 0 ; i < n ; i++) {
        nn = atoi(argv[i + 1]);
        b1 = bunsi * nn * nn + bunbo;
        b2 = bunbo * nn * nn;
        for(j = 2 ; j < nn * nn ; j++) {
            while(1) {
                sw = 0;
                if((b1 % j) == 0 && (b2 % j) == 0) {
                    b1 /= j;
                    b2 /= j;
                    sw = 1;
                }
                if(sw == 0) break;
            }
        }
        bunsi = b1;
        bunbo = b2;
    }
    printf("%d / %d\n" , bunsi , bunbo);
    return(0);
}


第52回(7月)ラマヌジャンを越えろ

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    long sum;
    int  n1;
    int  n2;
} V;
sort(int nrec , V v[])
{
    unsigned int *ilv , *ilh; /* ソート範囲 /*
    int  ibfsav;              /* ソートキーデータ /*
    V itmp;                   /* データのスワップ用 /*
    int  ip;                  /* カレントなソート範囲を指すポインタ /*
    int  il,ir;               /* カレントなソート範囲 /*
    int  i,j;                 /* ソート範囲のカウンタ /*
    int  ipiv;                /* 比較の対象値 /*
    int  size;                /* メモリーサイズ /*
    size = sizeof(int) * nrec;
    ilv = (unsigned int *)malloc(size);
    ilh = (unsigned int *)malloc(size);
    ip = 0;
    *ilv = 0;
    *ilh = nrec - 1;
    while(ip >= 0){
        il = *(ilv + ip);
        ir = *(ilh + ip);
        ip--;
        while(il < ir){
            i = il;
            j = ir;
            ipiv = (i + j) / 2;
            ibfsav = v[ipiv].sum;
            while(i <= j){
                while(v[i].sum < ibfsav) i++;
                while(v[j].sum > ibfsav) j--;
                if(i <= j){
                    itmp.sum = v[i].sum;
                    v[i].sum = v[j].sum;
                    v[j].sum = itmp.sum;
                    itmp.n1 = v[i].n1;
                    v[i].n1 = v[j].n1;
                    v[j].n1 = itmp.n1;
                    i++;
                    j--;
                }
            }
            if(i < ir){
                ip++;
                *(ilv + ip) = i;
                *(ilh + ip) = ir;
            }
            ir = j;
        }
    }
    free(ilv);
    free(ilh);
} 
int main(int argc , char **argv)
{
    unsigned long m , mm;
    unsigned long i , k = 0;
    unsigned long n1 , n2 , sum;
    V *v;
    if(argc != 2) {
        fprintf(stderr , "使用方法 : %s 数\n" , argv[0]);
        exit(0);
    }
    m = atoi(argv[1]);
    mm = m * m;
    v = (V *)malloc(mm * sizeof(V));
    for(i = 0 ; i < mm ; i++) v[i].sum = 0L;
    for(n1 = 1 ; n1 < m ; n1++) {
        for(n2 = 1 ; n2 <= n1 ; n2++) {
            sum = n1 * n1 * n1 * n1 + n2 * n2 * n2 * n2;
            v[k].sum = sum;
            v[k].n1 = (int)n1;
            v[k].n2 = (int)n2;
            k++;
        }
    }
    sort(k , v);
    for(i = 0 ; i < k ; i++) {
        if(i > 0) {
            if(v[i].sum == v[i - 1].sum) {
                printf("%ld = %ld ** 4 + %ld ** 4 = %ld ** 4 + %ld ** 4\n" ,
                    v[i].sum , v[i - 1].n1 , v[i - 1].n2 ,
                    v[i].n1 , v[i].n2);
                exit(0);
            }
        }
    }
    return(0);
}