题目
因式分解
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
将大于1的自然数N进行因式分解,满足N=a1a2a3…am。
编一程序,对任意的自然数N(1< N<=2,000,000,000),求N的所有形式不同的因式分解方案总数。如N=12,共有8种分解方案,他们分别是:
12=12
12=62
12=43
12=34
12=322
12=26
12=232
12=22*3
Input
输入只有一行,一个整数N。
Output
输出只有一行,一个整数表示自然数N的因式分解方案总数。
Sample Input
12
Sample Output
8
代码
#include<stdio.h>
#include<stdlib.h>
int count = 1; //下面的循环不包括1*n=n的情况
void Factorization(int n){
int i;
for(i = 2; i < n; i++){
if(n%i==0){
Factorization(n/i);
count++;
}
}
}
void main(){
int n;
scanf("%d",&n);
Factorization(n);
printf("%d\n",count);
system("pause");
}
困扰了我好久,刚开始想要找出所有的质数,然后一个一个循环的去试,直到最后的结果也是质数,但是添麻烦了,把自己给绕晕了,后来在网上看到这个算法思想,感觉很简洁,很直接。