[問題] 全域變數在遞迴裡執行結果
小弟最近開始寫一個題目為質因數分解,有宣告一個全域變數要讓遞迴一直降低測資的值
,可是只有在遞迴裡時值才會變,讓我百思不得其解,想請板上高手指導,以下為程式碼。
#include<stdio.h>
#include<stdlib.h>
int a=0;//此行為輸入測資之全域變數,在進行下面遞迴處理時假設測資為12,只有在
static int count;//遞迴裡a才會減少,當return時進入下一個for此時a又再回到12
int main()
{
int th(int x,int y);
int i,temp=0;
scanf("%d",&a);
printf("%d=",a);
for(i=2;i<=a;i++)
{
if(a==1)
break;
temp=th(a,i);
if(temp==1)
printf("%d*",i);
else if(temp!=0)
printf("%d^%d*",i,temp);
count=0;
}
system("pause");
return 0;
}
int th(int a,int b)
{
if((a%b)==0)
{
count++;
a=(a/b);
return th(a,b);
}
return count;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.126.50.174
→
09/16 19:12, , 1F
09/16 19:12, 1F
→
09/16 19:27, , 2F
09/16 19:27, 2F
→
09/16 19:28, , 3F
09/16 19:28, 3F
→
09/16 19:59, , 4F
09/16 19:59, 4F
→
09/16 20:00, , 5F
09/16 20:00, 5F
→
09/16 20:10, , 6F
09/16 20:10, 6F
→
09/16 20:19, , 7F
09/16 20:19, 7F