递归算法求幂

1. 迭代算法

public static int power(int n,int m){
		int temp=1;
		for(int i=0;i<m;i++)
			temp*=n;
		return temp;
	}

2. 递归算法 

    n^m == n^((m/2) * 2)

    n^m == (n^(m/2))^2

   因此采用递归算法:

   m为偶数: n^m = n^(m/2) *  n^(m/2)

    m为奇数: n^m = n^(m/2) * n^(m/2) * n

public class Power {
	public static void main(String[] args) {
		int n=2;
		int m=4;
		System.out.println(power(n,m));	
	}
	public static int power(int n,int m){
		if(m==0) return 1;
		if(m==1) return n;
		return m%2 == 0?power(n,m/2)*power(n,m/2):power(n,m/2)*power(n,m/2)*n;
	}
}

3.两者比较:

(1)、迭代算法空间复杂度O(1),时间复杂度 O(m)

(2)、递归算法空间复杂度O(logm),时间复杂度 O(logm)

(3)、递归要快于迭代但需要更大的空间。




在C++中,有多种方式可以使用递归算法集合。以下是几种实现方: #### 方一:回溯集 ```cpp #include <iostream> #include <list> using namespace std; typedef list<int> set; /**输出集合元素*/ void put_list(set A) { set::iterator p; for (p = A.begin(); p != A.end(); ++p) cout << *p << " "; cout << endl; } /*获取集合某一位置的元素*/ void GetElem(set A, int i, int &x) { set::iterator p = A.begin(); int j = 1; while (j < i) { p++; j++; } x = *p; } /*用回溯集*/ void getPowerSet(int i, set A, set &B) { int x; if (i > A.size()) put_list(B); else { GetElem(A, i, x); //将A[i]赋给x B.push_back(x); getPowerSet(i + 1, A, B); B.pop_back(); getPowerSet(i + 1, A, B); } } int main() { set A, B; int n, elem; cout << "源集合A元素个数:"; cin >> n; cout << "输入集合A的元素:"; while (n--) { cin >> elem; A.push_back(elem); } getPowerSet(1, A, B); return 0; } ``` 此方中,`getPowerSet`函数是核心的递归函数,通过不断地选择和不选择当前元素,递归地生成集的所有子集。当遍历完所有元素时,输出当前子集。该方的实现思路源自引用[3]。 #### 方二:线性表 + 先序遍历构造集 ```cpp #include <iostream> using namespace std; // 定义链表节点 struct LNode { int data; LNode* next; LNode(int val) : data(val), next(nullptr) {} }; typedef LNode* LinkList; // 输出链表元素 void Display(LinkList L) { LNode* p = L; if (!(p->next)) { cout << "NULL,"; return; } else { p = p->next; while (p) { cout << p->data; p = p->next; } } cout << endl; } // 链表长度 int ListLength(LinkList L) { int len = 0; LNode* p = L->next; while (p) { len++; p = p->next; } return len; } // 获取链表第i个元素 bool GetElem(LinkList L, int i, int& e) { int j = 1; LNode* p = L->next; while (p && j < i) { p = p->next; j++; } if (!p || j > i) return false; e = p->data; return true; } // 在链表第i个位置插入元素 bool ListInsert(LinkList& L, int i, int e) { int j = 0; LNode* p = L; while (p && j < i - 1) { p = p->next; j++; } if (!p || j > i - 1) return false; LNode* s = new LNode(e); s->next = p->next; p->next = s; return true; } // 删除链表第i个位置元素 bool ListDelete(LinkList& L, int i, int& e) { int j = 0; LNode* p = L; while (p->next && j < i - 1) { p = p->next; j++; } if (!(p->next) || j > i - 1) return false; LNode* q = p->next; p->next = q->next; e = q->data; delete q; return true; } // 集 void PowerSet(int i, LinkList A, LinkList& B) { int k = 0; int e = 0; if (i > ListLength(A)) { Display(B); } else { GetElem(A, i, e); k = ListLength(B); ListInsert(B, k + 1, e); PowerSet(i + 1, A, B); ListDelete(B, k + 1, e); PowerSet(i + 1, A, B); } } int main() { LinkList A = new LNode(0); LinkList B = new LNode(0); // 初始化集合A int n; cout << "源集合A元素个数:"; cin >> n; cout << "输入集合A的元素:"; for (int i = 0; i < n; i++) { int elem; cin >> elem; ListInsert(A, ListLength(A) + 1, elem); } PowerSet(1, A, B); return 0; } ``` 该方使用链表来表示集合,`PowerSet`函数是递归核心,通过先插入当前元素递归,再删除当前元素递归的方式生成集。此方的实现思路参考引用[2]。 #### 方三:使用向量实现的回溯集 ```cpp #include <vector> #include <iostream> using namespace std; void get_powerset(int i, vector<int> A, vector<int>& B) { int x; if (i > A.size() - 1) { if (!B.empty()) { cout << "{"; for (vector<int>::iterator it = B.begin(); it != B.end(); it++) { if (it != B.end() - 1) cout << *it << ","; else cout << *it; } cout << "}" << endl; } else cout << "Φ" << endl; } else { x = A[i]; B.push_back(x); get_powerset(i + 1, A, B); B.pop_back(); get_powerset(i + 1, A, B); } } int main() { int a[] = { 1, 2, 3 }; vector<int> ivec1(a, a + 3); vector<int> ivec2; get_powerset(0, ivec1, ivec2); return 0; } ``` 此方使用向量来存储集合元素,`get_powerset`函数递归生成集。其实现思路参考引用[4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值