0
Help to make a program that defines the perfect numbers in the one-dimensional array?
Transform it so that the imperfect numbers deduced the sum of their divisors. The source and the converted array of display
1 Réponse
+ 1
Оригинал:
#include <iostream>
using namespace std;
int main() {
int a[] = {2, 3, 4, 5, 6, 28, 496};
for(int i = 0; i < sizeof a / sizeof *a; ++i) {
int sum = 0;
for(int j = 1; j <= a[i] / 2; ++j)
sum += !(a[i] % j) * j;
if(sum == a[i])
cout << a[i] << "\tis perfect number" << endl;
}
}
Ремейк:
#include <iostream>
using namespace std;
int main() {
int a[] = {2, 3, 4, 5, 6, 28, 496};
for(int i = 0; i < sizeof a / sizeof *a; ++i) {
int sum = 0;
for(int j = 1; j <= a[i] / 2; ++j)
sum += !(a[i] % j) * j;
if(sum == a[i])
a[i] = 0;
else
a[i] = sum;
cout << a[i] << ' ';
}
}
Правильно задание понял?