一个非零的自然数可以被分解为多个二的乘幂之和是一个著名的数学定理
答案:1 悬赏:50 手机版
解决时间 2021-01-24 21:47
- 提问者网友:寂寞梧桐
- 2021-01-24 14:45
一个非零的自然数可以被分解为多个二的乘幂之和是一个著名的数学定理
最佳答案
- 五星知识达人网友:孤独的牧羊人
- 2021-01-24 15:28
#include
using std::cin;
using std::cout;
using std::endl;
class Array {
protected:
int* a;
public:
const int len;
public:
Array(const int& n) :len(n) {
this->a = new int[n];
}
inline int& operator[](int& ix) {
return this->a[ix];
}
inline int operator[](int& ix)const {
return this->a[ix];
}
Array(const Array& a) :len(a.len) {
this->a = new int[a.len];
for (int i = 0; i < a.len; i++) {
this->a[i] = a.a[i];
}
}
Array& operator=(const Array& a) {
*const_cast(&this->len) = a.len;
delete[] this->a;
this->a = new int[a.len];
for (int i = 0; i < a.len; i++) {
this->a[i] = a.a[i];
}
}
~Array() {
delete[] a;
}
};
//分解
Array factorial(int M) {
int n = 0;
int M1 = M;
for (int k = 2; k < M; k++) {
if ((M % k) == 0) {
n++;
M = M / k;
k = 1;
}
}
n++;
Array arr(n);
n = 0;
for (int k = 2; k < M1; k++) {
if ((M1 % k) == 0) {
arr[n] = k;
n++;
M1 = M1 / k;
k = 1;
}
}
arr[n] = M1;
return arr;
}
int main() {
int size;
cout << "输入个数:";
cin >> size;
int* n = new int[size];
for (int i = 0; i < size; i++) {
cout << "请输入一个正整数:";
cin >> n[i];
}
for (int i = 0; i < size; i++) {
Array a = factorial(n[i]);
for (int i = 0; i < a.len; i++) {
cout << a[i] << " ";
}
cout << endl;
}
delete[] n;
system("pause");
return 0;
}
using std::cin;
using std::cout;
using std::endl;
class Array {
protected:
int* a;
public:
const int len;
public:
Array(const int& n) :len(n) {
this->a = new int[n];
}
inline int& operator[](int& ix) {
return this->a[ix];
}
inline int operator[](int& ix)const {
return this->a[ix];
}
Array(const Array& a) :len(a.len) {
this->a = new int[a.len];
for (int i = 0; i < a.len; i++) {
this->a[i] = a.a[i];
}
}
Array& operator=(const Array& a) {
*const_cast
delete[] this->a;
this->a = new int[a.len];
for (int i = 0; i < a.len; i++) {
this->a[i] = a.a[i];
}
}
~Array() {
delete[] a;
}
};
//分解
Array factorial(int M) {
int n = 0;
int M1 = M;
for (int k = 2; k < M; k++) {
if ((M % k) == 0) {
n++;
M = M / k;
k = 1;
}
}
n++;
Array arr(n);
n = 0;
for (int k = 2; k < M1; k++) {
if ((M1 % k) == 0) {
arr[n] = k;
n++;
M1 = M1 / k;
k = 1;
}
}
arr[n] = M1;
return arr;
}
int main() {
int size;
cout << "输入个数:";
cin >> size;
int* n = new int[size];
for (int i = 0; i < size; i++) {
cout << "请输入一个正整数:";
cin >> n[i];
}
for (int i = 0; i < size; i++) {
Array a = factorial(n[i]);
for (int i = 0; i < a.len; i++) {
cout << a[i] << " ";
}
cout << endl;
}
delete[] n;
system("pause");
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯