幂函数的递归算法

#include<stdio.h>
int fun(int n,int k)
{
	int count=0;

	if(k==1)
	{
		return n;	
	}
	else
	{
		--k;
		count=n*fun(n,k);
		return count;

	}

}
int main()
{
	int a=2,b=3;
//	printf("请输入两个整数\n");
//	scanf("%d%d",&a,&b);
	int rat=fun(a,b);
	printf("%d\n",rat);
	return 0;
}

在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、付费专栏及课程。

余额充值