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 .
Am intr-o culegere de probleme de informatica de la biblioteca codul in PASCAL dar nu chiar asa cum e problema asta.Nu stiu sa scriu conditia.
Generarea combinarilor
Fiind date 2 nr n si k sa se genereze toate combinarile 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.
[Non-English content removed because we can't verify the content. --pbmods] -
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):boolean;
-
{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.AddItem 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
[Please use CODE tags when posting source code. Thanks! --pbmods] 1 1758 pbmods 5,821
Recognized Expert Expert
Removed email links.
Non-English portions of your post were removed because I could not verify the content. Please review the Posting Guidelines.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andrew |
last post by:
Last night I was reading about implementing my own stack. The example
given pushes items on and off the stack at the start and end of each
procedure (ie. in a std module). What's not so clear is how this
would work with class objects. In this case do you have to push the
object on the stack at the start of every public procedure etc. in the
class and pop it off at the end? I can't see how else you can know
which object is active - or...
|
by: Steven Spear |
last post by:
Hi. I am trying to perform backtracking with this search function.
Something is going wrong when I enter 2 at the command line. Entering
1 at the command line seems to work fine. I notice that backtracking
never takes place. Obviously there is something wrong with my logic. I
have been trying many things for many days to correct the situation,
but nothing seems to help. Does anyone have any suggestions? Thanks,
Steve
#include...
|
by: Chris Mabee |
last post by:
Hello all, and Merry Christmas,
I'm having a problem understanding an example of an array based
implementation of a stack in a textbook of mine. The code in question
is written below. The syntax is directly as in the book, except for
where I added the comments at the lines I wanted to refer to or to skip
sections of code.
template <class Element_Type>
class Stack
{
|
by: shoo |
last post by:
I am writing a program on Backtracking recursion in search of the
gold.
but I don't know how to start my search at location (1,1), which
should be Island in a empty space character..
Here is what I have done so far:
http://yuricoco.yu.ohost.de/111.cpp
and I should mrak very point that I have searched...like this
http://yuricoco.yu.ohost.de/finish.txt
|
by: Talin |
last post by:
I've been using generators to implement backtracking search for a while
now. Unfortunately, my code is large and complex enough (doing
unification on math expressions) that its hard to post a simple
example. So I decided to look for a simpler problem that could be used
to demonstrate the technique that I am talking about.
I noticed that PEP 255 (Simple Generators) refers to an implementation
of the "8 Queens" problem in the lib/test...
| |
by: ChuckB |
last post by:
Ok, I'm working on this program to do recursive backtracking, however
it's not working for all cases. Any help would be apprietiated. (all 4
should be true, but 168 is coming up false)
Heres the rules:
1. If n is even, then you may give back exactly n/2 bears.
2. If n is divisible by 3 or 4, then you may multiply the last two
digits of n and give back this many bears. By the way, the last digit
of n is n%10, and the next-to-last digit...
|
by: NOO Recursion |
last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a
thing called a "blob". A blob in the grid is made up of asterisks. A blob
contains at least one asterisk. If an asterisk is in a blob, an asterisk
that is contiguous to it is in the same blob. If a blob has more than two
asterisks, then each asterisk in the blob is contiguous to at least one
other asterisk in the blob. For example this 12x12 grid has 6 blobs. ...
|
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....
|
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
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...
|
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,...
| |
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |