题目如下:一个数如果恰好等于它的因子之和,这个数就称为“完数“。例如,6的因子为1,2,3,而6=1+2+3,因此6是“完数”。编程序找出1000以内的所有“完数”,并按下面的格式输出其因子:6 Its factors are 1,2,3 |
|
10分 |
设个断点调试。
l += a[k];这句话中k越界了,你数组a只有20个空间,k远远大于20 |
10分 |
if (l == i)
{ printf(“%d its facrors are”, i); for (m = 0; m<k; m++) printf(“%d,”, a[m]); |
80分 |
修改如下:
int main() { int i, j, m, k, l; //int a[20]; int a[1000]; for (i = 2; i <= 1000; i++) { k = l = 0; memset(a, 0, sizeof(int) * 1000); for (j = 1; j < i; j++) { if (i % j == 0) { a[k] = j; l += a[k]; k++; } } if (l == i) { printf("%d its facrors are ", i); for (m = 0; a[m + 1]; m++) printf("%d,", a[m]); printf("%d\n", a[m]); } } return 0; } //6 its facrors are 1, 2, 3 //28 its facrors are 1, 2, 4, 7, 14 //496 its facrors are 1, 2, 4, 8, 16, 31, 62, 124, 248 |