整数的素数和分解问题

from:  http://blog.csdn.net/alexingcool/article/details/6773442


对于一个给定的整数,输出所有这种素数的和分解式,对于同构的分解只输出一次(比如5只有一个分解2+3,而3+2是2+3的同构分解式)。

example:

对于整数8,可以作为如下三种分解:

(1) 8 = 2 + 2 + 2 + 2

(2) 8 = 2 + 3 + 3

(3) 8 = 3 + 5

 

看到此题时,我的头一反应是求解背包问题

思路:

f(N, array) = f(N - array[i], array), 保存结果,array是保存里面元素值,即所有素数,参考前面一题,如果素数只能唯一使用一次,那么就建立对应的一个bool数组即可,每使用一次就标记为true,然后递归函数之后需要重新置为false,对于本题不需要如此,但是需要将保存结果的数组除去当前尝试的素数。

代码不难写出:

 

[cpp]  view plain copy
  1. /*    
  2. * Copyright (c) 2011 alexingcool. All Rights Reserved.    
  3. */   
  4. #include <iostream>  
  5. #include <iterator>  
  6. #include <algorithm>  
  7. #include <vector>  
  8.   
  9. using namespace std;  
  10.   
  11. vector<int> result;  
  12. vector<int> prvec;  
  13.   
  14. void outputResult(int N, vector<int> &prime, vector<int> &result)  
  15. {  
  16.     if(N < 0)  
  17.         return;  
  18.   
  19.     if(N == 0) {  
  20.         copy(result.begin(), result.end(), ostream_iterator<int>(cout, " "));  
  21.         cout << endl;  
  22.         return;  
  23.     }  
  24.   
  25.     for(int i = 0; i < prime.size(); i++) {  
  26.         //为提高效率,可以在此做个判定条件,尽快返回  
  27.         if(N - prime[i] < 0)  
  28.             break;  
  29.   
  30.         result.push_back(prime[i]);  
  31.         outputResult(N - prime[i], prime, result);  
  32.         result.pop_back();  
  33.     }  
  34. }  
  35.   
  36. void outputResult2(int N, vector<int> &prime, vector<int> &result, int position)  
  37. {  
  38.     if(N < 0)  
  39.         return;  
  40.   
  41.     if(N == 0) {  
  42.         copy(result.begin(), result.end(), ostream_iterator<int>(cout, " "));  
  43.         cout << endl;  
  44.         return;  
  45.     }  
  46.   
  47.     for(int i = position; i < prime.size(); i++) {  
  48.         //为提高效率,可以在此做个判定条件,尽快返回  
  49.         if(N - prime[i] < 0)  
  50.             break;  
  51.   
  52.         result.push_back(prime[i]);  
  53.         outputResult2(N - prime[i], prime, result, i);  
  54.         result.pop_back();  
  55.     }  
  56. }  
  57.   
  58. bool isPrime(int number)  
  59. {  
  60.     if(number <= 1)  
  61.         return false;  
  62.     if(number == 2)  
  63.         return true;  
  64.   
  65.     for(int i = 2; i < number; i++) {  
  66.         if(number % i == 0)  
  67.             return false;  
  68.     }  
  69.   
  70.     return true;  
  71. }  
  72.   
  73. void generatePrime(int number, vector<int> &result)  
  74. {  
  75.     for(int i = 2; i < number - 1; i++) {  
  76.         if(isPrime(i))  
  77.             result.push_back(i);  
  78.     }  
  79. }  
  80.   
  81. void main()  
  82. {  
  83.     int number = 8;  
  84.     generatePrime(number, prvec);  
  85.     outputResult(number, prvec, result);  
  86.     cout << "除去同构" << endl;  
  87.     outputResult2(number, prvec, result, 0);  
  88. }  


结果:

 

PS:对于同构问题,我是看输出结果之后想到的,outputResult函数中,结果332,这样不对的结果,一个明显的特征是出现3后,其后面的数不能再小于3,那么只需要对保存3当前的position即可,然后在当前position循环,就可以消除同构问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值