Advertisement
nux95

currency permutation

Jan 13th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. /* Copyright (c) 2014  Niklas Rosenstein
  2.  *
  3.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  4.  * of this software and associated documentation files (the "Software"), to deal
  5.  * in the Software without restriction, including without limitation the rights
  6.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7.  * copies of the Software, and to permit persons to whom the Software is
  8.  * furnished to do so, subject to the following conditions:
  9.  *
  10.  * The above copyright notice and this permission notice shall be included in
  11.  * all copies or substantial portions of the Software.
  12.  *
  13.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19.  * THE SOFTWARE. */
  20.  
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include <string.h>
  25. #include <errno.h>
  26.  
  27. /* Parses a string and converts it to an integral number with base 10.
  28.  * Any characters after the first number is ignored.
  29.  *
  30.  * @param   c   A pointer to the string.
  31.  * @param  res  A pointer to the memory that will be filled with the
  32.  *              resulting value.
  33.  * @return true if the string could be converted, false if its contents
  34.  *         can not be interpreted as an integral number with base 10. */
  35. bool parse_int(const char* c, int* res) {
  36.     if (!c || !res)
  37.         return false;
  38.  
  39.     char mul = 1;
  40.     switch (*c) {
  41.         case '-':
  42.             mul = -1;
  43.         case '+':
  44.             c++;
  45.             break;
  46.     }
  47.  
  48.     *res = 0;
  49.     size_t count = 0;
  50.     while (*c != 0) {
  51.         if (*c < '0' || *c > '9')
  52.             break;
  53.  
  54.         *res = (*res * 10) + (*c - '0');
  55.         c++;
  56.         count++;
  57.     }
  58.  
  59.     *res *= mul;
  60.     return count > 0;
  61. }
  62.  
  63. /* Datastructure that contains the set of available coin values and the
  64.  * value for which the number of permutations is searched for. */
  65. typedef struct coindef {
  66.     size_t n;
  67.     const int* values;
  68.     int max;
  69. } coindef_t;
  70.  
  71. /* Count the numnber of permutations that are possible with the
  72.  * specified variation of coins. The coin values must be sorted in
  73.  * ascending order. */
  74. int count_permutations(int sum, int start, const coindef_t* coindef) {
  75.     if (sum > coindef->max)
  76.         return 0;
  77.     else if (sum == coindef->max)
  78.         return 1;
  79.  
  80.     int result = 0;
  81.     for (int i=start; i < coindef->n; i++) {
  82.         result += count_permutations(sum + coindef->values[i], i, coindef);
  83.     }
  84.     return result;
  85. }
  86.  
  87.  
  88. int usage() {
  89.     printf("USAGE: max\n");
  90.     return EINVAL;
  91. }
  92.  
  93. static const int coin_values[] = {1, 2, 5, 10, 20, 50, 100, 200};
  94.  
  95. int main(int argc, char** argv) {
  96.     if (argc != 2)
  97.         return usage();
  98.  
  99.     coindef_t coindef;
  100.     coindef.n = 8;
  101.  
  102.     // Read the first argument (which is coin max number) from the
  103.     // command-line.
  104.     if (!parse_int(argv[1], &coindef.max)) {
  105.         usage();
  106.         printf("max: must be a number.\n");
  107.         return EINVAL;
  108.     }
  109.  
  110.     coindef.values = coin_values;
  111.  
  112.     // Compute the number of permuations.
  113.     printf("%d permutations are possible.\n", count_permutations(0, 0, &coindef));
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement