Contestant and Gladiator problem

You are consulting for a game show in which n contestants are pitted against n gladiators in order to see which contestants are the best. The game show aims to rank the contestants in order of strength; this is done via a series of 1-on-1 matches between contestants and gladiators. If the contestant is stronger than the gladiator, then the contestant wins the match; otherwise, the gladiator wins the match. If the contestant and gladiator have equal strength, then they are “perfect equals” and a tie is declared. We assume that each contestant is the perfect equal of exactly one gladiator, and each gladiator is the perfect equal of exactly one contestant. However, as the gladiators sometimes change from one show to another, we do not know the ordering of strength among the gladiators.

The game show currently uses a round-robin format in which matches are played and contestants are ranked according to their number of victories. Since few contestants can happily endure gladiator confrontations, you are called in to optimize the procedure.

You are asked to give a reasonable representation for the input of the problem and give a randomized algorithm for ranking the contestants, whose expected number of matches should be . Besides the original program, a detailed design reported is required.

 

[code]

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int Rand(int p,int r){
    time_t t;
    srand((unsigned) time(&t));
    return  p + (rand() % (r-p+1));
}

void swap(int &i,int &j)
{
 int tmp = i;
 i = j;
 j = tmp;
}

int Partition(int * A,int p,int q ,int b)
{
 int i = p-1;
 int k = 0;
 for(int j = p; j <= q; j++)
 {
  if(A[j] < b)
  {
   i = i+1;
   swap(A[i],A[j]);
  }
  if(A[j] == b)
   k = j;
 }
    i++;
 swap(A[i],A[k]);
 return i;
}

void QuickSort(int * C,int p,int q,int * G)

 
 if(p < q)
 { 
  int rand = Rand(p,q);
  int m = Partition(G,p,q,C[q]);
  int n = Partition(C,p,q,G[m]);
  QuickSort(C,p,n-1,G);
  QuickSort(C,n+1,q,G);
 }
}

int main()
{
 int Contestant[8] = {3,7,4,6,1,5,8,2};
 int Gladiator[8] = {6,4,8,1,3,5,2,7};
 
    QuickSort(Contestant,0,7,Gladiator);
 
 printf("The sorted list of Contestant is: /n");
 for(int k = 0; k < 8; k++)
 {
  printf("%4d",Contestant[k]);
 }
 printf("/n");
 
 printf("The sorted list of Gladiator is: /n");
 for(int l = 0; l < 8; l++)
 {
  printf("%4d",Gladiator[l]);
 }
 printf("/nThe end! /n");
 return 1;
}

[code]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值