Timus Problem 1005 (Stone pile) Solution

Timus Problem Solution

Timus Problem 1005 (Stone pile):

This problem is for beginner in timus online judge.

Problem details link : http://acm.timus.ru/problem.aspx?space=1&num=1005

Problem Description:

just find the minimal difference among a value range.

Usage Tips:

>> Right shift operator : deletes the number of bits what are given from right
if 24>>2 then
24 binary = 11000 ,let right shift we get 110 which is 6.

<< Left shift operator: adds zero  bits for the given number on the right.
if 24<<2 then
24 binary = 11000 ,let left shift we get 10000 which is 16.

Left and right shift operator                                 Left and right shift operator example

Problem Solution:

#include<stdio.h>

int main()

 {

 int n, x[22], sum, ans, possible_combine, i;



 scanf("%d",&n);

  for(i=0; i<n; i++)

scanf("%d", &x[i]);

possible_combine = (1<<n);

 sum = 0;

 while(possible_combine)

 {

for(i=0; i<n; i++)

if(possible_combine & (1<<i))
{
        sum+=x[i];
}
else
{
sum-=x[i];
}
if(sum>=0 && ans>sum)

ans = sum;

sum = 0;

possible_combine--;

 }

 printf("%d\n", ans);

     return 0;

 }

0 Comments

You may find interest following article