You are not logged in. Please login at www.codechef.com to post your questions!

×

BESTBATS: Runtime Error

I am getting a runtime error for my code shown below.....cant figure out what is the problem it works fine on my compiler.

question:A cricket team consists of 11 players and some are good at batting, others are good at bowling and some of them are good at both batting and bowling. The batting coach wants to select exactly K players having maximum possible sum of scores. Given the batting score of each of the 11 players, find the number of ways in which we can select exactly K players such that the sum of their scores is the maximum possible. Two ways are different if there is a player who is selected in one of them is not in the other. See explanation of sample cases for more clarity. Input

First line contains T, number of test cases ( 1 â?¤ T â?¤ 100 ). T cases follow, each having 2 lines. First line of each case contains scores of 11 players ( 1 â?¤ score â?¤ 100 ) and the second line contains K (1 â?¤ K â?¤ 11) Output

For each test case, output the answer in a new line. Example

Input:

2

1 2 3 4 5 6 7 8 9 10 11

3

2 5 1 2 4 1 6 5 2 2 1

6

Output:

1

6

my approach: is first to sort the array of batsmen then select k out of them as per decreasing score. the last group(1 or more) of batsmen selected having the same scores will result in increased no of ways if there are othe rs having the same score but are not selected. so the no of ways comes only from the above group( last group: selected + non selected ). No of ways= C(total,selected) where total= selected + non selected.

include<stdio.h>

int cal_fact(int num); int get_arr(int arr); void sort_arr(int arr); void swap(int a,int b); int calc_ways(int *arr,int k);

void main(){

int arr[11]={0};
int t,k,ways;
scanf("%d",&t);
while(t--){

        get_arr(arr);
        scanf("%d",&k);
        sort_arr(arr);
        ways=calc_ways(arr,k);
        printf("%d\n",ways);

}

}

int cal_fact(int num){

if(num==0){
    return 1;
}
else{
    return num*cal_fact(num-1);
}

}

int get_arr(int *arr){int i; for(i=0;i<11;i++){ scanf("%d",&arr[i]); } }

void sort_arr(int *arr){ int i,j;

for(i=0;i<11;i++){
    for(j=0;j<11-i-1;j++){
        if(arr[j]<=arr[j+1]){
            swap(&arr[j],&arr[j+1]);
        }
    }
}

}

void swap(int a, int b){

*a=*a+*b;
*b=*a-*b;
*a=*a-*b;

}

int calc_ways(int *arr,int k){

int i,c_var,itr=0,count=0,selection=0;

c_var=arr[itr];
count++;
itr++;

while(itr!=k){
    if(arr[itr]!=c_var){
        c_var=arr[itr];
        count=1;
    }
    else{
        count++;
    }
    itr++;
}

selection=count;

for(i=k;i<11;i++){

    if(arr[i]==c_var){
        count++;
    }
    else{
        break;
    }
}

return (cal_fact(count)/cal_fact(selection))/cal_fact(count-selection);

}

asked 01 Oct '12, 16:05

tarun_gupta's gravatar image

2★tarun_gupta
1112
accept rate: 0%

edited 02 Oct '12, 01:32

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×525
×231
×11
×1

question asked: 01 Oct '12, 16:05

question was seen: 805 times

last updated: 02 Oct '12, 01:32