Header

  1. View current page

    정상혁의 수첩

Profile_img_60x60_08
192

코딩도장 첫번째 문제풀이 - 정동인, 최선재조

 

JUNIT의 TestCase를 이용하여 작성한 코딩도장 1번 carry문제풀이입니다.

 

public class CarryTest extends TestCase {

    public void testAdd() {

        Carry carry = new Carry();

        assertEquals(false, carry.isCarry(2, 3, 1));

    }

    public void testCarrySum() {

        Carry carry = new Carry();

        assertEquals(0, carry.getCarrySum(23, 31));

        assertEquals(2, carry.getCarrySum(23, 98));

        assertEquals(4, carry.getCarrySum(9923, 378));

        assertEquals(1, carry.getCarrySum(23, 38));

    }

}

 

public class Carry {

    public static void main(String[] args) {

        Carry carry = new Carry();

        System.out.println("input : " + args[0] + " " + args[1]);

        System.out.println("output: "

                + carry.getCarrySum(Integer.parseInt(args[0]), Integer

                        .parseInt(args[1])) + " carry operation(s)");

    }

    public boolean isCarry(int i, int j, int k) {

        if (i + j + k >= 10)

            return true;

        else

            return false;

    }

    public int getCarrySum(int i, int j) {

        int sum = 0;

        int maxLength = 0;

        int carry = 0;

        if (i > j)

            maxLength = (i + "").length();

        else

            maxLength = (j + "").length();

        for (int k = 0; k < maxLength; k++) {

            if (isCarry(i % 10, j % 10, carry)) {

                carry = 1;

                sum++;

            } else

                carry = 0;

            i /= 10;

            j /= 10;

        }

        return sum;

    }

}

 

History

Last edited on 04/17/2007 20:21 by benelog

Comments (0)

You must log in to leave a comment. Please sign in.