判断输出结果
在做练习的时候遇见的一道题,蒙了很长时间搞不懂其中的门道,大脑突然死机,哈哈。还好最后清楚了。
#include <stdio.h>
#include <stdlib.h>
void main()
{
struct node{
int x;
struct node *next;
}*p1,*p2=NULL;
int a[5]={7,6,-5,28,1},i,j,s;
for(i=0;i<5;i++){
s=0;
for(j=1;j<=a[i]/2;j++) //该循环循环完毕,才能跳出,这里主要是得出s的最终值
if(a[i]%j==0)
s+=j;
if(a[i]!=s){ //得出s的最终值后和a[i]比较,不相等就插入节点,这里是一个头插法
p1=(struct node*)malloc(sizeof(struct node));
p1->x=a[i];
p1->next=p2;
p2=p1;
}
}
while(p1!=NULL){
printf("%d\n",p1->x);
p1=p1->next;
}
}
输出结果:
结果:
1
-5
7
Press any key to continue