Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

Buy 1-Get 1 Wrong answer...!!!

$
0
0

http://www.codechef.com/problems/BUY1GET1 Above is the probelm of codechef, i tried to solve. I wrote the following code running perfectly fine on my laptop. Tried hard, but can't find any eroor for many other test cases also. Please Help.

My Algorithm - Declaring variables and an array as a count for 52 characters as mentioned i.e. 0-25 for upper case and 26-51 for lower case alphabets. Then, taking the no. of test cases. Then, using a loop to get string as a sequence of character until a newline is encountered. Now, using ASCII value of character of string, incrementing corresponding element of array. After this, calculating and printing the cost for each string using another loop and resetting the values of array elements.

Here is my source code(written in C):

#include <stdio.h>

int main(){
  int test,money,i,num[52] = {0};
  char ch = '1';
  scanf("%d",&test);
  getchar();
  while(test--){
    money = 0;
    while(ch!='\n'){
      scanf("%c", &ch);
      if((int)ch>91)
    num[(int)ch-71]++;
      else
    num[(int)ch-65]++;
    }
    ch = '1';
    for(i=0;i<52;i++){
      money += num[i]/2 + num[i]%2;
      num[i] = 0;
    }
    printf("%d\n", money);
  }
  return 0;
}

Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>