Problem: http://nabetani.sakura.ne.jp/hena/ordf04octsp/ Implementation links: http://qiita.com/Nabetani/items/8ead5622e192d9655cf5
As always, from the ruby implementation, which omits most of the test data:
def solve(src0)
  src = src0.to_i
  Array.new(8){ |s|
    if src[s]==1
      w = (1..8).find{ |x| src[(s+x)%8]==1 }
      w + (w==4 ? 1 : 2)
    end
  }.compact.sort.join
end
DATA.all?{ |line|
  num, src, expected = line.split(/\s+/ )
  actual = solve( src )
  okay = expected == actual
  puts( "%s %s:%s->%s ( %s )" % [(okay ? "ok" : "**NG**"), num, src, actual, expected] )
  okay
}.tap{ |x| puts( x ? "everything is okay" : "something wrong" ) }
__END__
0 165 3445
1 80  48
2 255 33333333
A straightforward implementation that you can spin around using the remainder. It's easier to handle bits with ruby.
The part where the number of vertices changes when the center becomes a straight line is ad hoc.
so.
The following is a port of this to C99 as it is:
//clang -std=c99 -Wall
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int compare( const void * a, const void * b ){
  const char * ac = (const char * )a;
  const char * bc = (const char * )b;
  return *ac-*bc;
}
char * solve_impl( int src )
{
  char buffer[9]={0};
  char * pos=buffer;
  for( int s=0 ; s<8 ; ++s ){
    if ( src & (1<<s ) ){
      int w=1;
      for( ; w<=8 ; ++w ){
        if ( src & (1<<((w+s)%8)) ){
          break;
        }
      }
      *pos=w+( w==4 ? '1' : '2' );
      ++pos;
    }
  }
  qsort( buffer, pos-buffer, 1, compare );
  return strdup( buffer );
}
char * solve( char const * src )
{
  return solve_impl( atoi( src ) );
}
void test( char const * src, char const * expected )
{
  char *  actual = solve( src );
  _Bool ok = 0==strcmp(actual, expected);
  printf( "%s : %s / %s / %s\n", (ok  ? "ok" : "**NG**"), src, expected, actual );
  free( actual );
}
int main()
{
  /*0*/ test( "165", "3445" );    
  /*1*/ test( "80", "48" );    
  /*2*/ test( "255", "33333333" );
  return 0;
}
Sorting is troublesome. It's really annoying.
Other than that, I think it's like this.
I think that the area where the character string is created by adding '1' and '2' is like C language.
Recommended Posts