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

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...

Database basic overview

What is DBMS? A Database Management System (DBMS) is a collection of interrelated data and a set of programs to access those data. Database management systems (DBMS) are computer software applications that interact with the user, other applications, and the database itself to capture and analyze data. Purpose of Database Systems The collection of data, usually...