ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

AC : Recursion. Don't see it unless you can't go anymore!!
Posted by za-Nuker 16 Apr 2005 19:31
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:
Posted by za-Nuker 16 Apr 2005 19:33
And so, sorry for my worst English ;-)