Thread: Perfect stairs problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Perfect stairs problem

    On the test that I'm studying for I have a problem to solve but I don't know how to solve that. The problem is the following:

    I have various piles of square stones and I need to organize that to make the most perfect possible stairs. There must be 1 stone of difference between each pile. The smallest pile must be always on the left. I'll give a picture to help (the pile of stone is on the left and the organizada pile is on the right):

    http://img180.imageshack.us/img180/9316/testth.png

    Does anyone know a known algorithm to do this?

    Thank you,
    Rafael Andreatta

    EDIT: Ops I forgot to mention what's my input format.

    I have the number of piles of stones and the number of stones in each pile.
    Last edited by Taturana182; 03-22-2010 at 05:31 PM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    You should have shown some code... Useless to help elsewise... Kind of! :P

    But, I made one little thing up for you, hope it helps you in the right direction!

    Code:
    #include <iostream>
    #include <iomanip>
    
    #include <stdio.h>
    using namespace std;
    
    //Global
    int stones[10]; //10 steps, goes from 0 to 9, 10 doesnt count :P
    
    void sortout()
    {
        //Order so the "Left pile stone", the index 0 (stones[0]) is the minimum value
        int tempvariable;
        for(int i = 0; i < 10; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                if(stones[i] < stones[j]) //Stones [j] is larger than [i], switch those two
                {
                  tempvariable = stones[i];
                  stones[i] = stones[j];
                  stones[j] = tempvariable;
                }
            }
        }
    
        for(int i = 0; i < 10; i++)
        {
                cout << "After rearrangement, lowest first: " << stones[i] << endl;
        }
    }
    
    
    int main()
    {
        int ii;
        for(int i = 9; i > -1; i--)
        {
            //Creating each 10 steps, initialized to value 9,8,7,6,5,4...
            stones[i] = i;
            cout << "Step " << stones[i] << endl;
        }
    
        //call sorting out, so left is stones[0], lowest pile of stones.
        sortout();
    
        return 0;
    }
    After you run this little program, then you can draw "stones[0]" to the left, which will have the lowest amount of piles.
    Last edited by ManyTimes; 03-22-2010 at 06:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM