unsigned int* remove(unsigned int* Arr, size_t* Size)
{
if (Arr == NULL)
return Arr;
unsigned int prfNums[41] = {2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521,
607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209,
44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, 2976221,
3021377, 6972593, 13466917, 20996011, 24036583};
unsigned int tmpArr[41] = {0};
unsigned int notPrfNumsCount = 0;
std::set<unsigned int> prfNumsSet(prfNums, prfNums+41);
for (size_t i =0; i < *Size; ++i)
{
std::set<unsigned int>::iterator it = prfNumsSet.find(Arr[i]);
if (it == prfNumsSet.end())
{
tmpArr[notPrfNumsCount] = Arr[i];
notPrfNumsCount++;
}
}
*Size = notPrfNumsCount;
unsigned int *resArr = new unsigned int[notPrfNumsCount * sizeof(unsigned int)];
std::copy(tmpArr, tmpArr+notPrfNumsCount,resArr);
return resArr;
}
В линейном динамическом массиве уничтожить все совершенные числа. Совершенное число (сумма делителей = самому числу) Например 6 = 1 +2 +3
Надо на с++
Совершенные числа выглядят немного иначе: 6 (1+2+3), 28 (1+2+4+7+14), 496 (1+2+4+8+16+31+62+124+248), 8128, 33550336, 8589869056, 137438691328...
Код:
unsigned long double* remove(unsigned long double* Arr, size_t* Size)
{
if (Arr == NULL)
return Arr;
unsigned long double prfNums[47] = {6,28,496,8128,33550336,8589869056,137438691328};
unsigned long double tmpArr[47] = {0};
unsigned int notPrfNumsCount = 0;
std::set<unsigned long double> prfNumsSet(prfNums, prfNums+7);
for (size_t i =0; i < *Size; ++i)
{
std::set<unsigned long double>::iterator it = prfNumsSet.find(Arr[i]);
if (it == prfNumsSet.end())
{
tmpArr[notPrfNumsCount] = Arr[i];
notPrfNumsCount++;
}
}
*Size = notPrfNumsCount;
unsigned long double *resArr = new long double[notPrfNumsCount * sizeof(unsigned long double)];
std::copy(tmpArr, tmpArr+notPrfNumsCount,resArr);
return resArr;
}
{
if (Arr == NULL)
return Arr;
unsigned long double prfNums[47] = {6,28,496,8128,33550336,8589869056,137438691328};
unsigned long double tmpArr[47] = {0};
unsigned int notPrfNumsCount = 0;
std::set<unsigned long double> prfNumsSet(prfNums, prfNums+7);
for (size_t i =0; i < *Size; ++i)
{
std::set<unsigned long double>::iterator it = prfNumsSet.find(Arr[i]);
if (it == prfNumsSet.end())
{
tmpArr[notPrfNumsCount] = Arr[i];
notPrfNumsCount++;
}
}
*Size = notPrfNumsCount;
unsigned long double *resArr = new long double[notPrfNumsCount * sizeof(unsigned long double)];
std::copy(tmpArr, tmpArr+notPrfNumsCount,resArr);
return resArr;
}
Хотя, кто знает, может, сегодня ночью станет чертовски скучно, и займусь этим.
Их можно взять тут.
Код:
#include "stdafx.h"
#include <iostream>
using namespace std;
bool isPerfect(unsigned __int64 uiVal)
{
unsigned __int64 uiSum = 0;
for (unsigned __int64 i = uiVal / 2; i >= 1; --i)
{
if (uiVal % i == 0)
{
uiSum += i;
}
}
return uiSum == uiVal;
}
unsigned int removePerfect(unsigned __int64* pArr, unsigned int uiSize, unsigned __int64** ppArrNew)
{
unsigned int uiCount = 0;
if (pArr != NULL || ppArrNew != NULL || uiSize != 0)
{
for (unsigned int i = 0; i < uiSize; i++)
{
if (isPerfect(pArr[i]))
{
*ppArrNew = (unsigned __int64*) realloc(*ppArrNew, (uiCount + 1) * sizeof(__int64));
(*ppArrNew)[uiCount++] = pArr[i];
}
}
}
return uiCount;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned __int64 uiArr [] = {3343, 5, 28, 496, 44, 6, 8128, 65, 345, 1, 33550336};
unsigned __int64* pArrNew = NULL;
unsigned int uiCount = removePerfect(uiArr, sizeof(uiArr) / sizeof(__int64), &pArrNew);
for (unsigned int i = 0; i < uiCount; i++)
{
cout << pArrNew[i] << endl;
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
bool isPerfect(unsigned __int64 uiVal)
{
unsigned __int64 uiSum = 0;
for (unsigned __int64 i = uiVal / 2; i >= 1; --i)
{
if (uiVal % i == 0)
{
uiSum += i;
}
}
return uiSum == uiVal;
}
unsigned int removePerfect(unsigned __int64* pArr, unsigned int uiSize, unsigned __int64** ppArrNew)
{
unsigned int uiCount = 0;
if (pArr != NULL || ppArrNew != NULL || uiSize != 0)
{
for (unsigned int i = 0; i < uiSize; i++)
{
if (isPerfect(pArr[i]))
{
*ppArrNew = (unsigned __int64*) realloc(*ppArrNew, (uiCount + 1) * sizeof(__int64));
(*ppArrNew)[uiCount++] = pArr[i];
}
}
}
return uiCount;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned __int64 uiArr [] = {3343, 5, 28, 496, 44, 6, 8128, 65, 345, 1, 33550336};
unsigned __int64* pArrNew = NULL;
unsigned int uiCount = removePerfect(uiArr, sizeof(uiArr) / sizeof(__int64), &pArrNew);
for (unsigned int i = 0; i < uiCount; i++)
{
cout << pArrNew[i] << endl;
}
system("pause");
return 0;
}