Board logo

标题: C语言经典算法之质因子 [打印本页]

作者: 苹果也疯狂    时间: 2014-5-22 09:33     标题: C语言经典算法之质因子

#include <stdio.h>
#include <stdlib.h>
struct node
{
         longfactorarray;
         structnode *next;
};
struct node *head,*q,*p;
void factoring(long n)
{
long i;
for(i=2; i<=n/2; i++)
{
         if(n%i==0)
         {  
                   q=(structnode*)malloc(sizeof(struct node));
                   q->factorarray=i;
                   q->next=NULL;
                   if(head==NULL)
                   {
                            head=q;
                            p=q;
                   }
                   else
                   {
                            p->next=q;
                            p=q;
                   }
         }
}
}
void deletes()
{
         while(head!=NULL)
         {
                  p=head;
                   head=p->next;
                   free(p);
         }
}
void main()
{
long num;
head=NULL;
printf("Please input the number youwant to find factors: ");
while(scanf("%ld", &num))
{
printf("The factors of the number %ld is: \n", num);
factoring(num);
p=head;
while(p!=0)
{
printf("* %ld", p->factorarray);
p=p->next;
}
printf("\nPlear input the number you want to find factors:");
deletes();
}
}




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0