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

1 problem 2 solutions

$
0
0

This is in reference to the problem FACTORIAL (Problem code FCTRL) link Factorial

One of solution got accepted:

import java.io.*;
import java.util.*;

 public class Main {
 public static void main(String [] args) throws IOException{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String num = br.readLine();
  for (int i = 0; i < Integer.parseInt(num); i++){
   int counter = 0;
   int number = Integer.parseInt(br.readLine());
   int div = 5;
   while (div <= number){
    counter += number/div;
    div *= 5;
   }
   System.out.println(counter);

  }
 }
}

while the other is giving wrong answer:

    import java.util.*;
    import java.io.*;
    public class Main{

    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        int T = Integer.parseInt(br.readLine());
        int[] a = new int[T];
        for(int i=1; i<=T; i++){
            int N = Integer.parseInt(br.readLine());
            a[i-1]=N;}
        br.close();
        for(int i=0; i<T; i++){
            System.out.println(""+zeroes(a[i]));}}

    public static int highest(int p, int N){
        int result=p;
        int count=1;
        while(result*p<N){
            result*=p;
            count++;}
        return count;}

    public static int zeroes(int N){
        int power= highest(5,N);
        int result=0;
        int num=5;
        for(int i=1; i<=power; i++){
            result+=N/num;
            num*=5;}
        return result;}
}

But both solutions are basically the same, just different ways of writing. Also both give the same result on many inputs I have tried on my compiler. So why one one of them(first one) got accepted while the other(second one) is giving "wrong answer" according to codechef ?


Viewing all articles
Browse latest Browse all 39796

Trending Articles



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