整数因子分解问题
2016-03-30 14:33
357 查看
问题描述:
大于1的正整数n可以分解为n = x1 * x2 * …*xm;
例如,当n = 12 时,共有8种不同的分解式:
12 = 12
12 = 2 *6
12 = 4 *3
12 = 3*4
12 = 3*2*2;
12 = 6*2
12 = 2*3*2
12 = 2*2*3
对于给定的正整数n,计算n共有多少种不同的分解式.
一开使想着迭代加深呢,,,,
不过直接dfs即可,每层先输出队列中的数字,再输出最有一项商.
大于1的正整数n可以分解为n = x1 * x2 * …*xm;
例如,当n = 12 时,共有8种不同的分解式:
12 = 12
12 = 2 *6
12 = 4 *3
12 = 3*4
12 = 3*2*2;
12 = 6*2
12 = 2*3*2
12 = 2*2*3
对于给定的正整数n,计算n共有多少种不同的分解式.
一开使想着迭代加深呢,,,,
不过直接dfs即可,每层先输出队列中的数字,再输出最有一项商.
/************************************************************************* > File Name: chaifen.cpp > Author: dulun > Mail: dulun@xiyoulinux.org > Created Time: 2016年03月30日 星期三 13时14分27秒 ************************************************************************/ #include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> #include<algorithm> #define LL long long using namespace std; const int N = 50086; int sum ; int a ; int n; int ans = 0; void dfs(int cur, int k) { ++ans; for(int i = 0; i < cur; i++) { printf("%d * ", a[i]); } printf("%d = %d\n", k, n); for(int i = 2; i < k; i++) { if( k % i == 0 ) { a[cur] = i; dfs(cur+1, k / i); } } } int main() { for(int i = 0; i <= N; i++) sum[i] = 1; cin>>n; dfs(0, n); //printf("%d * 1 = %d\n", n, n); printf("total = %d\n", ans); return 0; }
相关文章推荐
- Squirrel SQL Client连接hive安装部署
- 用青花瓷抓包
- View 生命周期
- 快速排序(JAVA描述)
- 块存储、文件存储、对象存储1
- 无线技术知识
- QWaitCondition类官方手册
- 整理苹果官网上iOS的各种辅助功能
- 如何做网络流量监控
- uber司机已经激活了,就是还没有上传头
- sysfs->sys简单介绍
- JPA下使用TransactionAwareDataSourceProxy带来的一个问题
- JavaScript中,for, while, if, switch用到的后缀递减(a--)运算
- Codeforces Round #339 D. Skills
- Java多线程基础:进程和线程之由来
- 3.4 Zend_Db_Table_Row
- SQL.WITH AS.公用表表达式(CTE)
- Dubbo的Maven打包
- Android 定时任务高度【schedule】与【scheduleAtFixedRate】差别
- 三层架构与MVC的区别