VC++编程题 编程实现对大于1的整数进行质因数分解,并求出其和编程实现对大于1的整数进行质因数分解,并求出其和.所谓整数的质因子分解是指将整数分解为其所有质数(素数)因数的积,例
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/18 10:37:58
![VC++编程题 编程实现对大于1的整数进行质因数分解,并求出其和编程实现对大于1的整数进行质因数分解,并求出其和.所谓整数的质因子分解是指将整数分解为其所有质数(素数)因数的积,例](/uploads/image/z/12552015-39-5.jpg?t=VC%2B%2B%E7%BC%96%E7%A8%8B%E9%A2%98+%E7%BC%96%E7%A8%8B%E5%AE%9E%E7%8E%B0%E5%AF%B9%E5%A4%A7%E4%BA%8E1%E7%9A%84%E6%95%B4%E6%95%B0%E8%BF%9B%E8%A1%8C%E8%B4%A8%E5%9B%A0%E6%95%B0%E5%88%86%E8%A7%A3%2C%E5%B9%B6%E6%B1%82%E5%87%BA%E5%85%B6%E5%92%8C%E7%BC%96%E7%A8%8B%E5%AE%9E%E7%8E%B0%E5%AF%B9%E5%A4%A7%E4%BA%8E1%E7%9A%84%E6%95%B4%E6%95%B0%E8%BF%9B%E8%A1%8C%E8%B4%A8%E5%9B%A0%E6%95%B0%E5%88%86%E8%A7%A3%2C%E5%B9%B6%E6%B1%82%E5%87%BA%E5%85%B6%E5%92%8C.%E6%89%80%E8%B0%93%E6%95%B4%E6%95%B0%E7%9A%84%E8%B4%A8%E5%9B%A0%E5%AD%90%E5%88%86%E8%A7%A3%E6%98%AF%E6%8C%87%E5%B0%86%E6%95%B4%E6%95%B0%E5%88%86%E8%A7%A3%E4%B8%BA%E5%85%B6%E6%89%80%E6%9C%89%E8%B4%A8%E6%95%B0%EF%BC%88%E7%B4%A0%E6%95%B0%EF%BC%89%E5%9B%A0%E6%95%B0%E7%9A%84%E7%A7%AF%2C%E4%BE%8B)
VC++编程题 编程实现对大于1的整数进行质因数分解,并求出其和编程实现对大于1的整数进行质因数分解,并求出其和.所谓整数的质因子分解是指将整数分解为其所有质数(素数)因数的积,例
VC++编程题 编程实现对大于1的整数进行质因数分解,并求出其和
编程实现对大于1的整数进行质因数分解,并求出其和.所谓整数的质因子分解是指将整数分解为其所有质数(素数)因数的积,例如,60=2*2*3*5,则整数60的质因数之和为12.定义一个类Decompose实现上述功能.
具体要求如下:
(1) 私有数据成员
●int *a:指向存放对应整数的质因数之和的动态存储空间.
●int *num:指向待分解质因数整数的动态存储空间.
●int n:整数的个数.
(2) 公有数据成员
●Decompose(int m,int b[]):用m初始化n,并用n初始化为动态申请空间的指针a与num.用参数b给数组a赋值.
●void primenum():求整数a[i]的所有质因数,并将这些质因数之和存放到指针num所指向的存储空间中.
●-Decompose():释放动态分配的存储空间.
(3) 在主函数中完成对该类的测试.从键盘输入一组大于1的整数,存放在number数组中,定义类Decompose的对象d,并用number初始化d,调用函数primenum()求number的所有质因数,最后输出测试结果.
VC++编程题 编程实现对大于1的整数进行质因数分解,并求出其和编程实现对大于1的整数进行质因数分解,并求出其和.所谓整数的质因子分解是指将整数分解为其所有质数(素数)因数的积,例
//
#define IS_PRINT_FACTOR 1 //定义是否打印中间结果
class Decompose
{
private:
unsigned __int64 *num;
unsigned __int64 *sum;
int n;
public:
Decompose();
Decompose(int n,unsigned __int64 *num);
virtual Decompose();
int primeNum(unsigned __int64 x,unsigned __int64 *fac);
void primeNum();
void printSum();
};
以下是cpp文件
////////////////////////////////////////////////////////////////////////////////////
// Decompose.cpp:implementation of the Decompose class.
//
//////////////////////////////////////////////////////////////////////
#include "Decompose.h"
#include "stdio.h"
#include "windows.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Decompose::Decompose()
{
num=NULL;
sum=NULL;
n=0;
}
Decompose::Decompose(int n,unsigned __int64 *num)
{
this->n=n;
this->num=new unsigned __int64[n];
sum=new unsigned __int64[n];
for(int i=0;inum[i]=num[i];
}
Decompose::Decompose()
{
if(num!=NULL)
delete[]num;
if(sum!=NULL)
delete[]sum;
}
unsigned __int64 sqrt64(unsigned __int64 x)
{
int i=(sizeof(unsigned __int64)>i)==1)
break;
i--;
}
return (unsigned __int64)11);
}
int Decompose::primeNum(unsigned __int64 x,unsigned __int64 *fac)
{
unsigned __int64 i=2,sqrtx=sqrt64(x);
int flen=0;
while(i