diff options
author | Haidong Ji | 2018-09-02 16:07:59 -0500 |
---|---|---|
committer | Haidong Ji | 2018-09-02 16:07:59 -0500 |
commit | 2a23f32bd6db02252509d0d9fdddc0b44b10056a (patch) | |
tree | 92bb97f9479ea607a8bf796d54ccb87e4a749928 | |
parent | 99b2699a067510ae42a968c760b87c3acd8daa9e (diff) |
Changing Money Greedy Algo done.
-rw-r--r-- | AlgoDesignAndTechniqueEdxJava/sources/ChangingMoney.java | 26 | ||||
-rw-r--r-- | AlgoDesignAndTechniqueEdxJava/tests/ChangingMoneyTest.java | 25 |
2 files changed, 51 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/sources/ChangingMoney.java b/AlgoDesignAndTechniqueEdxJava/sources/ChangingMoney.java new file mode 100644 index 0000000..08efc17 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/sources/ChangingMoney.java @@ -0,0 +1,26 @@ +import java.util.Scanner; + +public class ChangingMoney { + + public static int getNumOfCoins(int m) { + // coins with denominations of 1, 5, and 10 + int coinCount = 0; + int remainder; + if (m / 10 == 0 && m % 10 == 0) + return m / 10; + coinCount = coinCount + m / 10; + remainder = m % 10; + if (remainder >= 5) + return coinCount + 1 + remainder - 5; + else + return coinCount + remainder; + } + + public static void main(String args[]) { + Scanner in = new Scanner(System.in); + int m = in.nextInt(); + + System.out.println(getNumOfCoins(m)); + } + +} diff --git a/AlgoDesignAndTechniqueEdxJava/tests/ChangingMoneyTest.java b/AlgoDesignAndTechniqueEdxJava/tests/ChangingMoneyTest.java new file mode 100644 index 0000000..a6cb487 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/tests/ChangingMoneyTest.java @@ -0,0 +1,25 @@ +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class ChangingMoneyTest { + @Test + void testChangingMoney1() { + assertEquals(1, ChangingMoney.getNumOfCoins(1)); + } + @Test + void testChangingMoney2() { + assertEquals(2, ChangingMoney.getNumOfCoins(2)); + } + @Test + void testChangingMoney6() { + assertEquals(2, ChangingMoney.getNumOfCoins(6)); + } + @Test + void testChangingMoney28() { + assertEquals(6, ChangingMoney.getNumOfCoins(28)); + } + @Test + void testChangingMoney1000() { + assertEquals(100, ChangingMoney.getNumOfCoins(1000)); + } +} |