c 之遞迴小程式

2021-07-30 14:40:57 字數 1378 閱讀 3594

用遞迴方式完成以下程式

#include 

#include

using

namespace

std;

1.求前n個自然數之和

int

sum(int n)

void funtest()

void funtest()

int _data;

node* _next;

};void printlistfromtailtohead(node* phead)

else

}void funtest()

4.逆序銷毀單鏈表

void destoryfromtail(node* phead)

}void funtest()

5.在單鏈表中逆序查詢某個值為data的結點

node*

select(node* phead,int data)

}return

null;

}void funtest()

6.二分查詢遞迴形式

int binarysearch(int *arr, int left, int right, int data)

return -1;

}void funtest()

; int ret = binarysearch(arr, 0, 8, 17);

cout

<7.逆序列印陣列中的元素

void printarrayfromtailtohead(int* arr, int

size)

}void funtest()

; printarrayfromtailtohead(arr, 9);

}

8.判斷乙個字串是否為回文

bool ispalindromic(char* arr, int

left, int

right)

return true;

}void funtest()

9.研究perm是怎麼呼叫的

void perm(int* arr, int size, int n)

}}void funtest()

; perm(arr, 5, 3);

}

假設n = 3:

遞迴小程式二(C語言)

將非負十進位制整數n轉換成b進製。其中b 2 16 1 要實現進製的轉換,就是通過除法取餘數的方法,但通常得到的數都是要倒序輸出,所以通過函式的遞迴呼叫,在得到所有的位的計算結果後再輸出。2 若採用非遞迴方法,則需要用while 迴圈來計算,且計算結果用陣列存放,最後倒序輸出結果。拿十進位制轉二進位...

小程式之flex

display flex容器的屬性 flex direction flex wrap flex flow justify content align items align content flex direction屬性決定主軸的方向 即專案的排列方向 box flex direction屬性 r...

C程式入門小程式

c程式語言例題 includeint main printf the number of line is 3d nthe number of space is 3d nthe number of tab is 3d n line,space,tab return 0 includeint main ...