helloroot


私信TA

用户名:helloroot

访问量:10142

签 名:

等  级
排  名 5331
经  验 1491
参赛次数 0
文章发表 14
年  龄 0
在职情况 学生
学  校 上海交通大学
专  业

  自我简介:

解题思路:





注意事项:

我是在linux下面写的, factor[length]这个数组本来是和前面的变量定义在一起的.然后就编译,编译也没报错.然后就运行, 发现运行到第37次的时候,程序自动退出了,真的是太奇怪了. 后来让我朋友在MAC上编译了一下,发现有警告,length没有初始化.然后试着放下面, 就正确了.


参考代码:

#include <stdio.h>
int main()
{
    int length,i,j,count=0,sum=0;
    scanf("%d", &length);
    int factor[length];
    for(i=2;i<=length;i++,sum=0,count=0){
        for(j=1;j<i;j++){
            if( i% j == 0){
                factor[count]=j;
                count++;
            }
        }
        for(j=0;j<count;j++){
            sum+=factor[j];
        }
        if(sum == i){
            printf("%d its factors are", i);
            for(j=0;j<count;j++){
                printf(" %d", factor[j]);
            }
            printf("\n");
        }
    }
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区