LoginSignup
1
0

More than 5 years have passed since last update.

勉強会の参考問題の解答例。C言語で。

Last updated at Posted at 2012-12-26

先日投稿した参考問題( http://qiita.com/items/cbc3af152ee3f50a822f )の解答例。
C99で。

poka.c
/* use "-std=c99" */

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

#define ENDOF(ary) ( ary+sizeof(ary)/sizeof(*ary))

_Bool is_rank( char ch )
{
  return ! strchr( "SHDC1", ch );
}

int poka_key( char const * q )
{
  int lines=0;
  for( char const * from = q ; *from ; ++from ){
    if ( ! is_rank( *from ) ){
      continue;
    }
    for( char const * to=q ; to<from ; ++to ){
      if ( ! is_rank( *to ) ){
        continue;
      }
      if ( *from==*to ){
        ++lines;
      }
    }
  }
  return lines;
}

void poka( char const * q )
{
  int key=poka_key(q);
  puts( ((char const*[]){
    [6]="4K",
    [4]="FH",
    [3]="3K",
    [2]="2P",
    [1]="1P",
    [0]="--"
  })[key] );
}

int main()
{
  char const * q[]={
  "DASAD10CAHA",
  "S10HAD10DAC10",
  "S3S4H3D3DA",
  "SASJDACJS10",
  "H10D10H3HJCK",
  "S3SJDAC10SQ" };
  for( char const ** it=q ; it!=ENDOF(q) ; ++it ){
    poka(*it);
  }
}

やっぱりC言語だと長くなる。複合リテラルとか、指示付き初期化子とか使ってみた。
実装の方針は「同じランクのカードを線でつないでグラフを書いた時の、辺の数をかぞえると役がわかる」というもの。
カードが5枚だからたまたま成立する作戦で、カードが6枚だと、223344と222345が両方 3 になるのでうまくいかない。

ところで。
トラックバックの類はないのかなぁ。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0