473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

high school problem with LIFO

2 New Member
Dear mr. enginer
Please help me to rezolv this prolem.

We have some arrays:
A1 1 2 3 4 5 6 14 17 23 24 27 33
A2 7 11 13 17 19 20 25
A3 23 25 26 27
A4 34 45 46 47 48 49
A5 1 5 15 17 18 23 26 29 33 34 35 36 39 46 47
............... ............
............... ........
............... ........
A n
Another array B 2 3 4 5 23 25 33 34 44 45 46 (with maxim 15 nr. )
Combine NR. from B in n {it is necesary to be able to modificate n-ul } with condition to apear maxim z nr. {it is necesary to be able to modificate z-ul } from A1 ,maxim z nr. from A2 , A3 ETC .

I have a book with problems for high school but is not enough informations
to learn how to resolv this problem.The book is in Pascal anyway.

Generat the combinations :
if we have 2 nr n and k genarat all combinations de n luate cite k.


program R I4_6; {combimari de n cite k ne-recursiv}
type vector=array[1..25] of integer;
var st:vector; n,k:integer; {st=vectorul stiva}

procedure initializari; {initializeaza stiva si citeste n si k}
var I: integer;
begin
repeat
write ( ‘n=’)l readln (n);
write ( ‘k=’)l readln (k);
until n>=k;
for i:=1 to 25 do st[i]:=0;
end;

procedure tipar (p: integer) ; {tipareste o solutie memorata in vectorul st}
var i: integer;
begin
for i:=1 to p do
write (st [ i ] :4, ’ ’ ) ;
writeln;
end;

function valid (p: integer) : boolean;
{testeaza daca valoarea st[p] a generat o solutie valida, returnind true sau false}

var i: integer; ok:boolean;
begin
{valoarea de pe nivelul p nu trebuie sa se mai gaseasca pe nici unul din nivelele anterioare 1, 2,….,p-1}

ok:=true;
for i:=1 to p-1 do
if st[p]<=st[i] then
ok:=false;
valid:=ok;
end;

procedure back; {implementeaza algoritmul ne recursiv de backtracking}

var p: integer; {virful stivei}
begin
p:=1; st[p]:=0; {initializam primul nivel}
while p>0 do {cit timp stiva nu devine din nou vida}
begin
if st[p]<n then {daca mai exista vreo valoare neincercata pe nivelul p}
begin
{punem pe nivelul p urmatoarea valoare din multimea solutiilor posibile}

st[p]:= st[p]+1;
if valid(p) then {daca solutia (st[1],st[2],…st[p]) este valida}
if p=k then {daca solutia este si finala}
tipar (p) {tiparim solutia}
else
begin
p:=p+1; st[p]:=0; {trecem la nivelul urmator, pe care il re initializam}
end;
end
else {adica pe nivelul p nu se mai poate incerca nici o valoare}
p;=p-1; {pasul inapoi la nivelul anterior}
end;
end;

begin
initializari;
back;
end.
............... ............... ............... ............... ............... ............... ............... ............... ..
............... ............... ............... ............... ............... ............... ............... ............... ...........
Procedura recursiva {bktr(p:integer );} implementeaza algoritmul recursiv de backtracking tratind un nivel oarecare p al stivei.
-prin variabila pval vor trece pe rind , intr-un ciclu, toate valorile care teoretic ar putea fi puse pe nivelul p.
Ele sunt nr. naturale de la 1 la n .
Pentru fiecare dintre aceste valori :
--daca respectiva valoare a generat o solutie valida ( daca functia valid(p) a returnat TRUE ) , atunci:
--daca respectiva e si finala (p=k ) atunci o tiparim ( apelind procedura tipar(p) ) . In caz contrar , trecem la nivelul urmator ( pt. a completa solutia cu un nou nivel) , prin auto-apelul bktr(p+1) .


In programul principal ,apelam mai intai procedura initializari , apoi facem apelul bktr(1) care va declansa lantul de auto-apeluri.



Program RI4_2 ;
{COMBINARI DE N ELEMENTE LUATE CATE K }

type vector=array[1..25] of integer;
var st: vector; n,k:integer; {st=vectorul stiva}

procedure initializari;
var i: integer;
begin
for i:=1 to 25 do st[i]:=0; {initializeaza stiva}
{citeste k si n, cu validarea conditiei n>=k}

repeat
write (‘n=’); readln(n);
write (‘k=’); readln(k);
until n>=k;
end;


procedure tipar ( p:integer) ;
{tipareste o solutie memorata in vectorul st}
var j:integer;
begin
for j:=1 to p do
write (st[ j ]:4,’ ‘);
writeln ;
end;

function valid (p:integer):boo lean;
{testeaza daca valoarea pusa pe nivelul p generat o solutie valida, returnind true sau false}
var I :integer;
begin
{valorile de pe stiva trebuie sa fie in ordine crescatoare, deci st[p] trebuie sa fie mai mare decit elementul st[p-1] de pe nivelul anterior}
if (p>1) and (st[ p ]<=st[p-1]) then valid:=false
else valid:=true;
end;

procedure bktr (p:integer) ; {implementeaza algoritmul de backtracking}
var pval:integer;
begin
{in variabila pval trec pe rind toate valorile care ar putea fi incercate pe nivelul p}
for pval:=1 to n do
begin
st[p]:=pval; {pune o noua valoare pe nivelul p}
if valid(p) then {daca solutia obtinuta e valida}
if p=k then {daca este si finala(o solutie finala are k nivele)}
tipar (p) {tipareste solutia}
else
bktr (p+1); {trece la nivelul urmator in stiva , pt. a completa solutia}
end;
end;

begin
initilizari;
bktr (1) ; {plecam de la nivelul 1 pe stiva}
readln;
end.

……………………………………… ……………………………………… …………………………….
Thank you . The next code is corect ?? :


Dim st(100), n, k As Integer



Private Sub CommandButton1_ Click()
initializari
back

End Sub

Private Sub UserForm_Click( )

End Sub



Sub initializari() '/{initializeaza stiva si citeste n si k}

Do
n = Int(InputBox("n =?"))
k = Int(InputBox("k =?"))
Loop Until k <= n
For i = 1 To 25
st(i) = 0
Next i



End Sub

Sub tipar(p)
rez = ""
For i = 1 To p
rez = rez + Str(st(i))
Next i
ListBox1.AddIte m rez


End Sub

Function valid(p)
ok = True
For i = 1 To p - 1
If st(p) <= st(i) Then ok = False
Next i
valid = ok

End Function

Sub back() ' {implementeaza algoritmul ne recursiv de backtracking}

p = 1
st(p) = 0
While (p > 0)
If st(p) < n Then
st(p) = st(p) + 1
If valid(p) Then
If p = k Then
tipar (p)
Else
p = p + 1
st(p) = 0
End If
End If
Else
p = p - 1
End If


Wend


End Sub
Jul 20 '07 #1
0 1430

Sign in to post your reply or Sign up for a free account.

Similar topics

75
6182
by: Howard Nease | last post by:
Hello, everyone. I would appreciate any advice that someone could give me on my future career path. Here is my situation: I am a bright Junior in a very well-respected private high school, taking almost all AP and accelerated classes. I am HIGHLY interested in technology, more specifically the field of Computer Science and software engineering. I have heard a whole lot about the fact that the market for software engineers nowadays is...
16
4378
by: msnews.microsoft.com | last post by:
I am teaching C# to my 11 year old child. One challenge is that all the C# books I own and that I have seen in bookstores are full of language that is not easily comprehended by a student at that age. Can anyone recommend books (or perhaps websites) tuned for younger audiences? BTW, its amazing how fast a student can absorb this kind of information at that age. Lucky them! Thanks, Bruce
1
1204
by: LedZep | last post by:
This program has to use a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and punctuation. I have to somehow take the input from the txtbox, stack it letter by letter onto a stack, then display it written forward and below it written backwards. I understand the concept of stacks -- I just need a little push in the right...
1
2183
by: Skyer | last post by:
How to write? I have write program in C language. This program contain 1 array with struct. Every element of table must be dynamic list LIFO (stack). Adding and removing struct variable from list array. My question: How to declare this table? How to begining, any ideas? struct list { int *type;
0
724
by: Jan Claeys | last post by:
Op Thu, 03 Apr 2008 00:06:34 -0700, schreef Dennis Lee Bieber: Pointers in Borland's Pascal (and FreePascal) are bare machine pointers, with optional typing for the referenced value; I've never seen anything you could do with C pointers that you couldn't do with Borland Pascal pointers. (And I think the reason why pointers in C looked complicated is that the C syntax for pointers is inconsistent...)
0
928
by: luigileldsak | last post by:
patch high school class of 85 http://cracks.12w.net F R E E
1
2497
by: Kevin Killion | last post by:
I am building a website for a school, but running into a display problem between IE and CSS. (The site is http://stmary-stthomas.org/ ) The main horizontal menu bar looks just fine on Mac and PC with most browsers. But on the PC with IE7, the menu bar winds up about 20 pixels too high. The menu is drawn in a DIV with class "navbar", which is inside a DIV
5
2303
AmberJain
by: AmberJain | last post by:
HELLO, The book (which I'm presently referring) of C programming says- "A stack is an example of a LIFO (Last In First Out) structure, as opposed to, for example, a queue, which is a FIFO (First In First Out) structure." Now I have some questions about above statement: 1. What does "structure" in above statement stands for? 2. Is it possible to code a program which has the ability to extract data items from stack in any random order (i.e....
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprι who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.