sunnyrubik 发表于 2018-10-25 23:30:11

为什么会出现undefined reference呢?

本帖最后由 sunnyrubik 于 2018-10-26 00:02 编辑

(题目要求)
亲密的数字。如果整数A的全部因子(包括1,不包括A)之和等于B;且整数B的全部因子(包括1,不包括B)之和等于A,则将整数A和B称为亲密数。求n以内的全部亲密数。要求:
输入:n (n是不小于1的整数)
输出:1~n以内的全部亲密数。注意当输入条件不满足时,输出“Input error!”
提示:最小的一对亲密数是:220--284

#include<stdio.h>
#include<stdlib.h>
int yinzihe(int x);
int main()       
{
        int n;
        int a,b,mid;
        printf("请输入数字n\n");
        scanf("%d",&n);
        if(n<1){printf("您输入的是无效数字\n");}
        else
        {
                for(a=1;a<=n;++a)
                {
                        mid=yinzihe(a);
                        b=mid;
                        if(a==yinzihe(b)){printf("%d----%d",a,b);}
                       
                }
        }
        system("pause");
        return 0;
       
}
int yinzihe(int x)//定义函数yinzihe求整数A的全部因子(包括1,不包括A)
{
        int y,i=1,sum=0;
        scanf("%d",&y);
        for(y=y;i<y;++i)
        {
                if(0==y%i)
                {
                        sum+=i;
                }
        }return sum;
}


但是它不能运行出结果,这是为是什么呢?

claws0n 发表于 2018-10-25 23:30:12

sunnyrubik 发表于 2018-10-26 00:19
好像把函数换了就好了耶。但是感觉函数没写错啊?

随便输入了个数12,输出的结果是16哇,没问题啊{: ...

什么没问题?问题可大了
int x; printf("%d",yinzihe(x));   //x 是啥?没有初始化,垃圾值
for(y=y;i<y;++i)// 你去哪里学的??

最小的亲密数是 220 ----- 284,哪来的12,16??

claws0n 发表于 2018-10-25 23:33:01

拼写,yinzihe

sunnyrubik 发表于 2018-10-25 23:38:54

claws0n 发表于 2018-10-25 23:33
拼写,yinzihe

可是好像改了之后虽然编译通过了,但运行还是不行耶?您知道这是为什么吗?

claws0n 发表于 2018-10-26 00:01:27

sunnyrubik 发表于 2018-10-25 23:38
可是好像改了之后虽然编译通过了,但运行还是不行耶?您知道这是为什么吗?

不知道你在做什么{:10_250:}#include<stdio.h>
#include<stdlib.h>

int yinzihe(int x);

int main()         
{
    int n;
    int a,b,t;
    printf("请输入数字n\n");
    scanf("%d",&n);
    if(n<1)
                printf("您输入的是无效数字\n");
    else
    {
      for(a = 1; a <= n; ++a)
      {
            b = yinzihe(a);
            if(a == yinzihe(b) && a < b)
                printf("%d----%d\n",a,b);
               
      }
    }
    system("pause");
    return 0;
      
}

int yinzihe(int x)//定义函数yinzihe求整数A的全部因子(包括1,不包括A)
{
    int i, sum = 0;
    for(i = 1; i < x;++i)
    {
      if(0 == x%i)
            sum += i;
    }
        return sum;
}

sunnyrubik 发表于 2018-10-26 00:19:26

claws0n 发表于 2018-10-26 00:01
不知道你在做什么

好像把函数换了就好了耶。但是感觉函数没写错啊?#include<stdio.h>
#include<stdlib.h>
int yinzihe(int x);
int main()
{   
    int x;
        printf("%d",yinzihe(x));
        system("pause");
        return 0;
}
int yinzihe(int x)
{
        int y,i=1,sum=0;
        scanf("%d",&y);
        for(y=y;i<y;++i)
        {
                if(0==y%i)
                {
                        sum+=i;
                }
        }return sum;
}

随便输入了个数12,输出的结果是16哇,没问题啊{:10_291:}

sunnyrubik 发表于 2018-10-26 10:16:11

claws0n 发表于 2018-10-25 23:30
什么没问题?问题可大了
int x; printf("%d",yinzihe(x));   //x 是啥?没有初始化,垃圾值
for(y=y; ...

嗯嗯,在我的yinzihe()括号里输的数好像确实不起作用,看的是scanf的数。谢谢你啊(另外我的意思是说12的因子和是16,学校老师讲的太快了,自己反应比较慢,有些细节确实有点懵{:10_278:})

claws0n 发表于 2018-10-26 10:30:40

sunnyrubik 发表于 2018-10-26 10:16
嗯嗯,在我的yinzihe()括号里输的数好像确实不起作用,看的是scanf的数。谢谢你啊(另外我的意思是说12的 ...

计算机专业?怎么都教这些东西?
掌握分支与循环就可以啦,那些计算题上网找一找就好
《带你学C带你飞》带过地看一下,先了解大方向再慢慢补齐

sunnyrubik 发表于 2018-10-26 10:45:32

claws0n 发表于 2018-10-26 10:30
计算机专业?怎么都教这些东西?
掌握分支与循环就可以啦,那些计算题上网找一找就好
《带你学C带你飞 ...

嗯,跟计算机相关的专业,网安,谢谢你的建议,先把大框架弄起。
页: [1]
查看完整版本: 为什么会出现undefined reference呢?