|
|
back to boardhelp,please,why my solution fail? here is my code: #include <stdio.h> int total(int count, int sum) { int result = 0; if( count > 1 ) { int i=0; for (; i <= 9; i++) { result += total(count - 1, sum - i); } } else { return sum < 10 && sum >= 0 ? 1 : 0; } return result; } int main () { unsigned a; int n,i,c,b;
scanf("%ud", &a); b = a / 2 * 9; for(i=0; i <= b; i++ ) { c = total(a/2, i); n += c * c; } printf("%d", n);
return 0; } i tried 2,4,6,8,the result is correct,but when i submit my solution,[wrong answer] is returned... Edited by author 02.09.2011 14:58 Re: help,please,why my solution fail? i got it,in visual stdio 2010,auto variable must initialized before used Re: help,please,why my solution fail? Hello Smart :D, Can you please explain how does your algorithm works? I find out that the summation of digits is multiplication of 9 at most; but why (c* c) ???? ( please explain the whole intention behind your algo as well. Thanks Re: help,please,why my solution fail? Posted by killo 25 Jun 2012 16:20 Here is the correct code #include <stdio.h> int total(int count, int sum) { int result = 0; if( count > 1 ) { int i=0; for (; i <= 9; i++) { result += total(count - 1, sum - i); } } else { return sum < 10 && sum >= 0 ? 1 : 0; } return result; } int main () { unsigned a; int n = 0; int i,c,b; scanf("%ud", &a); b = a / 2 * 9; for(i=0; i <= b; i++ ) { c = total(a/2, i); n += c * c; } printf("%d", n); return 0; } Here is the correct code |
|
|