Professional Documents
Culture Documents
Lab4 Recursion
Lab4 Recursion
1. ศึกษารีเคอซีฟฟงกชั่น initArr และ run step สังเกตุดูตอน call recursion และหยุด call recursion
template<class T> //template int main() {
void initArr(T* a, int v, int i, int lastIndex){ const int MAX = 3;
//init a ดวยคา v ตัง้ แตอน
ิ เด็กซ i ถึง lastInd int a[MAX];
if (i <= lastIndex){ initArr(a, 5, 0, MAX-1);
a[i] = v; initArr(a, 3, 0, MAX-1);
initArr(a, v, i+1, lastIndex); // recursion
} }
}
2. เขียนรีเคอซีฟฟงกชั่น void initArrWithInd(T* a, int i, int lastIndex) เพื่อ init อะเรย a ดวยเลขอินเด็กซ คือ
a[0] = 0, a[1] = 1, … , a[lastIndex] = lastIndex
3. เขียนรีเคอซีฟฟงกชั่น void print(T* a, int i, int lastIndex) เพื่อพิมพอะเรย a ตั้งแตอินเด็กซ i ถึง lastIndex
4. เขียนรีเคอซีฟฟงกชั่น revPrint เพื่อพิมพอะเรย a ตั้งแตอินเด็กซ lastIndex ถึง i (คิดพารามิเตอรเอง)
การทดลองที่ 3 โปรแกรมขางลางใหคาตั้งตนและพิมพลิงคลิสต โดยใหเขียนฟงกชั่นเปนเทมเพลทของไทป T ใดๆ
หากคิดไมออก ศึกษาเฉลยขางหลัง เขียนฟงกชั่น แลวรันดู
1. เขียนรีเคอซีฟฟงกชั่น print เพื่อพิมพลิสตจากหัวไปทาย และ revPrint เพื่อพิมพลิสตจากทายมาหัว
int main() { /*
node<int> *h, *p, *t; h = t = NULL;
createListWithNodeNo(h, t, 1, 5);
h = t = new node<int>(1); */
for(int i=2; i<=4; i++){//creat list cout << endl; cout << "print: ";
p = new node<int>(i); print(h);
t->next = p; cout << "\nrevPrint: ";
t = p; revPrint(h);
} return 0;
}
2. คอมเมนตบรรทัดที่ไฮไลท และเอาคอมเมนต /* */ ออก เขียนรีเคอซีฟฟงกชั่น createListWithNodeNo
เพื่อสรางลิสตใหเหมือนบรรทัดที่คอมเมนตออกไป (คิดพารามิเตอรเอง)
if (i <= lastIndex){
a[i] = v;
initArr(a, v, i+1, lastIndex);//call recursion
}
}
template<class T>
void initArrWithInd(T* a, int i, int lastIndex){
//init array a with index number from index i to index lastIndex
if (i <= lastIndex){
a[i] = i;
initArrWithInd(a, i+1, lastIndex);//call recursion
}
}
template<class T>
void print(T* a, int i, int lastIndex){
//print a from index 0 to index MAX-1
if (i <= lastIndex){
cout << a[i] << ' ';
print(a, i+1, lastIndex);//call recursion
}
}
template<class T>
void revPrint(T* a, int i, int lastIndex){
//reverse print a from index MAX-1 to index 0
if (i <= lastIndex){
revPrint(a, i+1, lastIndex);//call recursion
cout << a[i] << ' ';
}
}
int main() {
const int MAX = 3;
int a[MAX];
if (p){
cout << p->data;
print(p->next);//call recursion
}
}
template<class T>
void revPrint(node<T> * p){
//reverse print from last node to p
if (p){
revPrint(p->next);//call recursion
cout << p->data;
}
}
template<class T>
void createListWithNodeNo(node<T> *& h, node<T> *& t, int i, int size){
node<T> *p;
if (h == NULL) {
h = t = new node<int>(i);
createListWithNodeNo(h, t, i+1, size); //call recursion
}
else if (i < size){
p = new node<int>(i);
t->next = p;
t = p;
createListWithNodeNo(h, t, i+1, size); //call recursion
}
}
int main() {
node<int> *h, *p, *t;
h = t = new node<int>(1);
for(int i=2; i<=4; i++){ //creating list
p = new node<int>(i);
t->next = p;
t = p;
}//now h & t points to head and tail of a list
/*
h = t = NULL;
createListWithNodeNo(h, t, 1, 5);
*/
cout << endl; cout << "print: "; print(h);
cout << "\nrevPrint: "; revPrint(h);
return 0;
}
void pick(int start, int m, int ns, int *sack){//ns=total items in a sack
for(int i=start; i<np; i++){//every combination of prices
sack[ns++] = p[i]; // pick to the sack
m -= p[i]; // pay
if (m == 0){ // got solution, use up money
numsol++;
for(int j=0; j<ns; j++) //print sack
cout<<sack[j]<<" ";
cout<<endl;
}else if (m>0 && i!=np) //still money & item left
pick(i+1,m,ns,sack);//go pick another item
ns--; //take item i out of the sack(for other solution)
m+=p[i];//return money for item i back
} //for
} //pick
void main(){
int sack[MAX]; //items(prices) in sack
int m, i; //m = money we got
cout << "\nEnter set of prices, end with 0 or Ctrl-Z then <Enter>: ";
np = 0;
while(cin>>i && i>0 && np<MAX)
p[np++] = i; //after the loop np = #items
cout << "How much you got? "; cin >> m; cout << endl;
pick(0,m,0,sack);//start pick 1st item at p[0], now 0 item in sack
if (!numsol) cout << "\nNo solution!\n";
}