Secretary Problem ( A Optimal Stopping Problem ) | C # Program to test 1 / e law for Secretary Problem ; To find closest integer of num . ; Finds best candidate using n / e rule . candidate [ ] represents talents of n candidates . ; Calculating sample size for benchmarking . ; Finding best candidate in sample size ; Finding the first best candidate that is better than benchmark set . ; Driver Code ; n = 8 candidates and candidate array contains talents of n candidate where the largest number means highest talented candidate . ; generating random numbers between 1 to 8 for talent of candidate
using System ; class GFG { static double e = 2.71828 ; static int roundNo ( float num ) { return ( int ) ( num < 0 ? num - 0.5 : num + 0.5 ) ; } static void printBestCandidate ( int [ ] candidate , int n ) { int sample_size = roundNo ( ( float ) ( n / e ) ) ; Console . WriteLine ( " STRNEWLINE STRNEWLINE Sample ▁ size ▁ is ▁ " + sample_size ) ; int best = 0 ; for ( int i = 1 ; i < sample_size ; i ++ ) if ( candidate [ i ] > candidate [ best ] ) best = i ; for ( int i = sample_size ; i < n ; i ++ ) if ( candidate [ i ] >= candidate [ best ] ) { best = i ; break ; } if ( best >= sample_size ) Console . WriteLine ( " found is " ( best + 1 ) + " ▁ with ▁ talent ▁ " + candidate [ best ] ) ; else Console . Write ( " Couldn ' t ▁ find ▁ a ▁ best ▁ candidate STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] candidate = new int [ n ] ; Random rand = new Random ( ) ; for ( int i = 0 ; i < n ; i ++ ) candidate [ i ] = 1 + rand . Next ( 1 , 8 ) ; Console . Write ( " Candidate ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( i + 1 + " ▁ " ) ; Console . WriteLine ( ) ; Console . Write ( " Talents ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( candidate [ i ] + " ▁ " ) ; printBestCandidate ( candidate , n ) ; } }