信息学奥赛一本通C++——T1200:分解因数

【题目描述】


        给出一个正整数a,要求分解成若干个正整数的乘积,即a=a1×a2×a3×…×an,并且1<a1≤a2≤a3≤…≤an,问这样的分解的种数有多少。注意到a=a也是一种分解。

【输入】
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1<a<32768)。

【输出】
n行,每行输出对应一个输入。输出应是一个正整数,指明满足要求的分解的种数。

【输入样例】
2
2
20
【输出样例】
1
4

【方法1】

#include<bits/stdc++.h>
using namespace std;
int tot;
int t=2;
void dfs(int x){
	if(x == 1){
		tot++;
		return;
	}
	for(int i=t;i<=x;i++){
		if(x%i == 0){
			t = i;
			dfs(x/i);	
		}
	}
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		tot = 0;
		t=2;
		dfs(x);
		cout<<tot<<endl;
	}
}

【方法2】


#include<bits/stdc++.h>
using namespace std;
int a[100];
int ans;
void dfs(int x,int step){
	if(x == 1){
		ans++;
//		for(int j=1;j<step;j++){
//			cout<<a[j]<<"  ";
//		}			
//		cout<<endl;
	}
	for(int i=a[step-1];i<=x;i++){
		if(x%i == 0){
			a[step++] = i;
			dfs(x/i,step);
			step -- ;
		}
	}
}
int main(){
	int n;
	a[0] = 2;
	cin>>n;
	dfs(n,1);
	cout<<endl<<ans;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值