[ create a new paste ] login | about

Link: http://codepad.org/mJ05BldC    [ raw code | output | fork ]

C++, pasted on Mar 10:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main( void ) {
  const char* s = "MCDXLIV";
  int x = 0; // result

  int j,m=0; // max used digit
  const char* p=s, *q; while(*p) ++p;
  for( --p; p>=s; p-- ) for( q="IVXLCDM",j=0; *q; q++,j++ ) if( *p==*q )
    x += ((j>=m)?m=j,1:-1) * (1+j%4/2*9) * (1+j/4*99) * (1+j%2*4);

  printf( "s=%s x=%i\n", s, x );
}


Output:
1
s=MCDXLIV x=1444


Create a new paste based on this one


Comments: