#UVa:12247-Jollo

王子要怎麼出都會贏的話,至少要兩張可以贏公主三張牌的牌組,或是要三張可以贏公主至少兩張牌的牌組,依這樣的規則就可以推出王子要的第三張牌會是什麼了。

C++(0.008)

/*******************************************************/
/* UVa 12247 Jollo                                     */
/* Author: LanyiKnight [at] knightzone.studio             */
/* Version: 2013/01/22                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

void printAnswer( int start, bool &print, bool cardused[] );

int main(){
  int princess[3], prince[3];
  while(scanf("%d%d%d%d%d", &princess[0], &princess[1], &princess[2],
                            &prince[0], &prince[1] ) != 0 ){
    if( princess[0] == 0 && princess[1] == 0 && princess[2] == 0
        && prince[0] == 0 && prince[1] == 0 )
      break;

    bool cardused[55] = {false};
    for( int i = 0 ; i < 3 ; ++i )
      cardused[princess[i]] = true;
    for( int i = 0 ; i < 2 ; ++i )
      cardused[prince[i]] = true;

    int minWinCount = 3, maxWinCount = 0;
    for( int i = 0 ; i < 2 ; ++i ){
      int winCount = 0;
      for( int j = 0 ; j < 3 ; ++j )
        if( prince[i] > princess[j] )
          ++winCount;
      minWinCount = min(minWinCount, winCount);
      maxWinCount = max(maxWinCount, winCount);
    }
    bool print = false;
    if( minWinCount == 3 ){
      printAnswer( 1, print, cardused );
    }
    else if( minWinCount == 2 ){
      int middle = princess[0] + princess[1] + princess[2] -
                  (min(princess[0], min(princess[1], princess[2])) +
                   max(princess[0], max(princess[1], princess[2])));
      printAnswer( middle+1, print, cardused );
    }
    else if( maxWinCount == 3 ){
      printAnswer( max(princess[0], max(princess[1], princess[2]))+1, print, cardused );
    }

    if( !print ) printf( "-1\n" );
  }
  return 0;
}

void printAnswer( int start, bool &print, bool cardused[] ){
  for( int i = start ; i <= 52 ; i++ ){
    if( !cardused[i] ){
      printf( "%d\n", i );
      print = true;
      cardused[i] = true;
      break;
    }
  }
}
廣告

Comment

There is no comment on this post. Be the first one.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

%d 位部落客按了讚: