|
Counting
|
|
08-18-2009, 08:00 PM
(This post was last modified: 05-09-2011 12:29 AM by drdebcol.)
Post: #1
|
|||
|
|||
|
Counting
Gustavo knows how to count, but he is just now learning how to write numbers.
He has already learned the digits 1, 2, 3, and 4. But he does not yet realize that 4 is different than 1, so he thinks that 4 is just another way to write 1. He is having fun with a little game he created: he makes numbers with the four digits that he knows and sums their values. For example: Code: 132 = 1 + 3 + 2 = 6Input Input will be an integer 1 ≤ n ≤ 1, 000. Output Output an single integer stating how many numbers Gustavo can make such that the sum of their digits is equal to n. Sample Input 2 Sample Output 5 Sample Input 3 Sample Output 13 You can find the solution just by making a for loop that will count and you just separate digits using MOD DIV algorithm, you have it explained here : http://www.pro9ramming.com/showthread.php?tid=124 And then check if numbers in that integer are 1,2,3 or 4 and that's it and the check sum of course. But this algorithm, despite it's slowness, is not elegant. So i decided to make recursive solution that is faster and more reliable. It basically makes every possible variation of 1,2,3 or 4 counting from length 1 to n, and checks for sum. So here is the code in TPW : Code: program counting;Code: #include <iostream>There's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
08-18-2009, 09:14 PM
Post: #2
|
|||
|
|||
|
RE: Counting
Typical mathematical task... Programing in this task is not so matter
Read rules ![]()
|
|||
|
08-18-2009, 11:44 PM
Post: #3
|
|||
|
|||
RE: Counting
(08-18-2009 09:14 PM)l3g1sl4tor Wrote: Typical mathematical task... Programing in this task is not so matterYeah, you can make variations with repetition using this formula : Code: V(n,k)=n!/(n-k)!There's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
« Next Oldest | Next Newest »
|






