AOJ 1115 Multi-column List

問題

Multi-clumn List

plen行、cnum列のリストを作成して出力する。各列の幅はwidth、列の間には幅cspaceのスペースを作る。

plen行出力した後は'#'を出力して、データセットの出力の最後に'?'を出力する。

サンプルのoutput見ればだいたい分かる。

解法

list[plen][cnumwidth + (cnum-1)cspace]の配列を使って、文字を格納する。

入力ごとにplenのインデックスを増やして、plenいっぱいになったらcnumのインデックスを増やす。cnumもいっぱいになったら出力して配列の初期化、次のページを作り始める。

心臓に悪いコード

int plen, cnum, width, cspace;
char res[128][64], line[1024];

int main(){
  while(scanf("%d", &plen) && plen){
    scanf("%d%d%d", &cnum, &width, &cspace);

    rep(i, plen){
      rep(j, cnum * width + cspace * (cnum-1)) res[i][j] = '.';
      res[i][cnum * width + cspace * (cnum-1)] = '\0';
    }

    int p = 0, c = 0;
    fgets(line, sizeof(line), stdin);
    while(1){
      fgets(line, sizeof(line), stdin);
      if(line[0] == '?') break;
      line[strlen(line)-1] = '\0';
      if(p == plen){ c++; p = 0;}
      if(c == cnum){
    rep(i, plen) printf("%s\n", res[i]);
    puts("#");
    p = c = 0;
    rep(i, plen) rep(j, cnum * width + cspace * (cnum-1)) res[i][j] = '.';
      }
      rep(i, strlen(line)){
    if(i && i%width == 0) p++;
    if(p == plen){ c++; p = 0;}
    if(c == cnum){
      rep(i, plen) printf("%s\n", res[i]);
      puts("#");
      p = c = 0;
      rep(i, plen) rep(j, cnum * width + cspace * (cnum-1)) res[i][j] = '.';
    }
    res[p][c*width+c*cspace+i%width] = line[i];
      }
      p++;
    }
    if(res[0][0] != '.'){
      rep(i, plen) printf("%s\n", res[i]);
      puts("#");
    }
    puts("?");
  }
  return 0;
}