Chapter IV recursive algorithm 1200: factorization

1200: factorization

Time limit: 1000 ms memory limit: 65536 KB
Submissions: 6248 Number through: 3519
[Title] Description
is given a positive integer a, requires a positive integer into several product, i.e. a = a1 × a2 × a3 × ... × an, and 1 <a1≤a2≤a3≤ ... ≤an, asked several such decomposition of the number. Noting also a decomposition of a = a.

[Input]
Line 1 is the set of n number of test data, followed by n input lines. Each set of test data representing a line, a positive integer including a (1 <a <32768) .

[Output]
n lines, each corresponding to an input line of output. The output should be a positive integer indicating the several decomposition meet the requirements.

[Input] Sample
2
2
20 is
[] sample output
. 1
. 4


Ideas: Recursive

#include<cstdio>
#include<iostream>
using namespace std;
int n,j;
int f(int a,int b)
{
	if(a==1) return 1;
	if(b==1) return 0;
	if(a%b==0)
	return f(a/b,b)+f(a,b-1);
	return f(a,b-1);	
 } 
 int main(){
 	   cin>>n;
 	for(int i=1;i<=n;i++)
 	{
 		cin>>j;
 		cout<<f(j,j)<<endl;
	 }
 }
Published 108 original articles · won praise 2 · Views 2061

Guess you like

Origin blog.csdn.net/zqhf123/article/details/104424445