2013-07-15 39 views
2

我已經爲因子分解一個Delphi程序,它看起來像這樣:輸出錯誤的因子分解

enter image description here

40是多少,當你點擊「Scomponi」你有正確的輸出(2^3 * 5 = 40)。我做了一個C程序應該做同樣的事情,但我有這樣的輸出:

enter image description here

正如你可以看到,右邊的數字是正確的(2^3 * 5),但數字在左邊沒有。這是我寫的代碼:

int main() 
{ 
long a,b=2; 
printf("------------------------------------------------ \n \n"); 
printf("Inserisci il numero di cui vuoi la scomposizione \n"); //input number (it's the 40 of the example) 
scanf("%d", &a); 
printf("\n------------------------------------------------ \n\n"); 
printf("Scomposizione: \n \n"); //Decomposition 
    while(a>1) 
    { 
    if(a%b == 0)  
    { 
    printf("%d \t \t | %d \n",a,b); 
    a=a/b; 
    } 
    else 
    { 
    b++;  
    } 
    printf("%d", a); 
    } 
    printf("\n------------------------------------------------ \n\n"); 
getch(); 
return 0;  
} 

我該如何解決這個問題?

+1

我想這個'printf(「%d」,a);'是多餘的。 – JeffRSon

回答

8
else 
    { 
    b++;  
    } 
    printf("%d", a); 
    } 

最後的printf不應該在這裏。去掉它。

作爲附帶說明:

ablong類型。

因此,而不是:你必須使用

scanf("%d", &a); 

scanf("%ld", &a); 

同爲printf,使用%ld轉換規範,而不是%d

+0

謝謝,它適用於我:) –

+1

@ DK64不客氣! – ouah