求解流程控制的問題


Recommended Posts


#include <stdio.h>
#include <stdlib.h>

int main()
{
int set,testa,testb,count,k,testc;
float testcf;

count=0;

printf("Set a integer:\n");
scanf("%d",&set);

[COLOR="Blue"] for(testa=2;testa<=set;testa++)[/COLOR]
{
for(testb=1;testb<=testa;testb++)
{
if(testa%testb==0)
{
count++;
}
}
if(count==2)
{
for(k=1;k<=set;k++)
{
testcf=pow(testa,k);
testc = (int) testcf;
if(set%testc==0)
{
[COLOR="Red"]printf("%d ",testa); [/COLOR]
}
}
}
}

system("PAUSE");
return 0;
}

請問如何在標示紅字的地方執行完後回到開頭(藍字)讓他繼續執行完第一個迴圈(testa<=set的那邊)?

目前的執行結果是輸入一個數字之後就不會跑了(只出現請按任意鍵繼續)...

※:程式想法

1.輸入set決定要質因數分解的數字

2.testa從1開始測試直到set

3.testb從1開始測試直到testa

4.取testa與testb的餘數

5.若為0則,count加1

6.若count=2,則判斷為質數,並且進行以下動作

7.取set與testa^k的餘數,若為0則輸出一次testa,若不為0則回到開頭繼續測試下一個testa(就是這裡出問題的)

※(這部分是先以pow(testa,k)處理得到一個testa^k(testcf)後,再將這個數從浮點數轉換成整數(testc),接著才進行測試)

請問哪裡有錯誤?或者有其他的質因數分解寫法?

此內容已被編輯, ,由 williamd
鏈接文章
分享到其他網站

#include <stdio.h>
#include <stdlib.h>

int main()
{
int set=0,testa=0,testb=0,testc=0;
bool count = true;



printf("Set a integer:\n");
scanf("%d",&set);

for(testa=2;testa<=set;testa++)
{
count = true; //初始值,預設testa為質數
for(testb=2;testb<testa;testb++) //判斷testa是否為質數
if(testa%testb==0){ //如果testa為質數,則count為true
count=false; //並離開迴圈
break;
}
if(count&&set%testa==0) //如果testa為質數並且set為testa之倍數
{ //則算出testa在set中的個數
testc = 1;
while(testc<=set)
{
testc *= testa;
if(set%testc==0) printf("%d ",testa);
}
}
}
printf("\n");
system("PAUSE");
return 0;
}

看不懂再PM問我吧

此內容已被編輯, ,由 伊達政宗
鏈接文章
分享到其他網站

請登入後來留意見

在登入之後,您才能留意見



立即登入