一个数如果恰好等于它的因子之和,这个数就被称为“完数”。
找出1000之内的所有完数,并且按下面格式输出其因子:
6,its factors are 1,2,3
#include<iostream> #include<String> using namespace std; int main() { for (int
i = 1; i < 1001; i++) { int y = 0; for (int x = 1; x < i; x++) { if (i % x ==
0) { y+=x; } } if (y == i) { int a = 1; cout << i <<",its factors are "; for
(int b = 1; b < i; b++) { if (i % b == 0&&a==1) { cout << b; a = 0; } else if(i
% b == 0){ cout << ',' << b; } } cout << endl; } } return 0; }
运行结果:
6,its factors are 1,2,3 28,its factors are 1,2,4,7,14 496,its factors are
1,2,4,8,16,31,62,124,248