|
|
вернуться в форумAC : Recursion. Don't see it unless you can't go anymore!! Because of no AC in this problem board, so I posted this for ones who are still getting WA and don't want to try more. If you are the one like solving yourself, please ignore this topic, thx. I used basic recursion for this problem : (Time 0.015, Mem 62 K) This is my AC program : #include <stdio.h> #include <math.h> int arr[20], sum = 0, min = -1, cal; void Solution(int i,int cur) { ....if(i == 20) ....{ ........cal = abs(2*cur-sum); ........if(min == -1) ........{ ............min = cal; ........} ........else ........{ ............if(cal < min) ................min = cal; ........} ....return; ....} ....Solution(i+1,cur+arr[i]); ....Solution(i+1,cur); } int main(void) { ....int n, i; ....scanf("%d",&n); ....for(i=0; i<n; i++) ....{ ........scanf("%d",&arr[i]); ........sum += arr[i]; ....} ....Solution(0,0); ....printf("%d",min); ....return 0; } Re: And so, sorry for my worst English ;-) |
|
|