NOTE

「あみだくじ」

「あみだくじ」yantene

One reaction at a time. Picking another replaces it.

部活で、同級生と「何かアルゴリズムやらんかー」と話していたところ、「どう書く?org」にてこんな問題を見つけました。

注意

どう書く?org は 2017 年 9 月の時点で既にサービスを終えていました。 2026 年 8 月に確かめたところ、ドメインは第三者に再取得されていたため、リンクを取り下げました。

ということで、その同級生は COBOL で、自分 C でこの「あみだくじ」なる問題を解いてみました。

学校の PC は Windows だったからでしょうか、 Linux のようにバッファにデータを貯めて単純に scanfで読み込んでいく方法が通用しませんでした。 scanf内に\nをいれてやったら動くようになりました。

#include <stdio.h>
#include <string.h>

int main(){
  char namae[5], temp, yokobo[4];
  int i, j;
  scanf("%c %c %c %c %c", &namae[0],  &namae[1], &namae[2], &namae[3], &namae[4]);
  for(i = 0; i < 5; i++){
    scanf("\n|%c|%c|%c|%c|", &yokobo[0], &yokobo[1], &yokobo[2], &yokobo[3]);
    for(j = 0; j < 4; j++){
      if((int) yokobo[j] == (int) '-'){
        temp = namae[j];
        namae[j] = namae[j+1];
        namae[j+1] = temp;
      }
    }
  }
  for(i = 0; i < 5; i++){
    printf("%c ", namae[i]);
  }
  scanf("%d", &i);
  return 0;
}

あんま美しいコードじゃないですが、ご容赦ください。 COBOL 版はその同級生のブログに後々掲載されるいハズです。

One reaction at a time. Picking another replaces it.