~cpp MyTest.java
¶~cpp import junit.framework.TestCase; public class MyTest extends TestCase { private HowManyZerosAndDigits object; public void testFactorial() { object = new HowManyZerosAndDigits(0, 0); assertEquals(1, object.factorial(1)); assertEquals(2, object.factorial(2)); assertEquals(6, object.factorial(3)); assertEquals(24, object.factorial(4)); } public void testHowManyZeros() { object = new HowManyZerosAndDigits(0, 0); assertEquals(0, object.howManyZeros(1)); assertEquals(1, object.howManyZeros(10)); assertEquals(2, object.howManyZeros(100)); assertEquals(2, object.howManyZeros(1010)); } public void testNumberSystemConversion() { // object = new HowManyZerosAndDigits(120, 16); // assertEquals(2, object.convertNumber()); // // object = new HowManyZerosAndDigits(120, 10); // assertEquals(3, object.convertNumber()); } }
~cpp HowManyZerosAndDigits.java
¶~cpp import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; public class HowManyZerosAndDigits { private int _n; private int _b; private int _fact; private LinkedList numbers; public HowManyZerosAndDigits(int n, int b) { _n = n; _b = b; numbers = new LinkedList(); } public void solve() { _fact = factorial(_n); convertNumber(); System.out.println(howManyZeros() + " " + numDigit()); } public int factorial(int n) { int result = 1; for(int i=1; i<n+1; ++i) { result = result*i; } return result; } public void convertNumber() { if( _b == 10 ) return; int quotient = _fact / _b; int remainder = _fact % _b; if( remainder != 0 ) numbers.add(new Long(remainder)); while( quotient != 0 ) { remainder = quotient % _b; quotient = quotient / _b; if( !(quotient==0 && remainder==0)) // 제일 상위자리가 0인 경우는 넣지 않는다. numbers.add(new Long(remainder)); } } public int howManyZeros(long num) { int count = 0; String number = Long.toString(num); for(int i=0; i<number.length(); ++i) { if( number.charAt(i)== '0' ) ++count; } return count; } public int howManyZeros() { if( _b == 10 ) { return howManyZeros((long)_fact); } else { int count = 0; Object arr[] = numbers.toArray(); for(int i=0; i<arr.length; ++i) { Long l = (Long)arr[i]; count += howManyZeros(l.longValue()); } return count; } } public int numDigit() { if( _b == 10 ) { int count = 1, value = _fact; value = value/_b; while( value != 0 ) { value = value/_b; ++count; } return count; } else { return numbers.size(); } } public static void main(String args[]) { try { int n = readInt(); int b = readInt(); HowManyZerosAndDigits h = new HowManyZerosAndDigits(n, b); h.solve(); } catch(Exception ex) { ex.printStackTrace(); } } public static int readInt() throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String intVal = reader.readLine(); return Integer.parseInt(intVal); } }