簡體   English   中英

編寫一個程序,計算並顯示用戶輸入的正整數 n 的階乘

[英]write a program that computes and displays the factorial of a positive integer n entered by the user

一直在課堂上研究這個問題。 不知道我做錯了什么。

好像我只是沒有正確的格式。 我的教授希望輸出看起來像“5!=1 * 2* 3* 4* 5=120”

有人可以幫我弄這個嗎? 以下是我到目前為止所擁有的:

#include <iostream> 
#include <stdio.h>

int main() {
    int n, i, fact;

    printf("Enter a positive integer: \n");
   scanf("%d", &n);
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        for( i = 1; i <= n; ++i) {
            fact *= i;
            n= n * (n-1);
        }
        printf("Factorial of %d = ", &n, &fact) ;    
    }

    return 0;
}
  1. 如果是C代碼,則刪除#include <iostream>
  2. 使用函數。
  3. 初始化局部變量(你沒有,這是未定義的行為)
  4. 使用 C 編譯器編譯 C 程序
unsigned long long fact(unsigned val, char *buff)
{
    unsigned result = 1;
    *buff++ = '1';
    *buff = 0;
    for(unsigned c = 2; c <= val; c++) 
    {
        buff += sprintf(buff, "x%u", c);
        result *= c;
    }
    return result;
}

int main(void)
{
    unsigned x;
    char y[1000];
    unsigned long long res;

    if(scanf("%u", &x) == 1){res = fact(x, y); printf("factoral(%u) = %s = %llu\n", x, y, res);}
    else printf("Invalid number!!!\n");
}

或沒有印刷步驟

unsigned long long fact(unsigned val)
{
    unsigned result;
    switch(val)
    {
        case 0: 
        case 1:
            result = 1;
            break;
        case 2:
            result = 2;
            break;
        default:
            result = 2;
            while(val > 2) result *= val--;
            break;
    }
    return result;
}

int main(void)
{
    unsigned x;

    if(scanf("%u", &x) == 1) printf("factioral(%u) = %llu\n", x, fact(x));
    else printf("Invalid number!!!\n");
}

https://godbolt.org/z/Tf5431zco

對於初學者來說,將變量 n 聲明為有符號整數類型是沒有意義的。 最好將其聲明為無符號整數類型。 例如

unsigned int n, i, fact;

其次,變量 fact 沒有被初始化。 您應該將其初始化為等於 1 的值。

所以上面的聲明可能看起來像

unsigned int n, i;
unsigned long long fact = 1;

在這個 for 循環中

    for( i = 1; i <= n; ++i) {
        fact *= i;
        n= n * (n-1);
    }

該聲明

        n= n * (n-1);

沒有意義,應刪除。

在 printf 調用中,您必須使用變量的值而不是指向變量的指針,例如

printf("Factorial of %u = %llu\n", n, fact) ;

注意頭文件<iostream>聲明都沒有在你的程序中使用 所以無論是 C 程序還是 C++ 程序,都刪除這個頭文件。

這是一個演示 C 程序。

#include <stdio.h>

int main( void )
{
    unsigned int n = 0;
    unsigned long long int fact = 1;

    printf( "Enter a positive integer: " );
    scanf( "%u", &n );

    printf( "%u! = ", n );

    for ( unsigned int i = 1; i <= n; i++ )
    {
        fact *= i;

        if ( i != 1 ) printf( " * " );

        printf( "%u", i );
    }

    printf( " = %llu\n", fact );
}

程序輸出可能看起來像

Enter a positive integer: 5
5! = 1 * 2 * 3 * 4 * 5 = 120

如果您只允許使用您在答案的評論中所寫的int類型的變量,則程序看起來像

#include <stdio.h>

int main( void )
{
    int n = 0;
    int fact = 1;

    printf( "Enter a positive integer: " );
    scanf( "%d", &n );

    if  ( n  <  0 )
    {
        puts( "Error! Factorial of a negative number doesn't exist."); 
    }
    else 
    {
        printf( "%d! = ", n );

        for ( int i = 1; i <= n; i++ )
        {
            fact *= i;

            if ( i != 1 ) printf( " * " );

            printf( "%d", i );
        }

        printf( " = %d\n", fact );
    }   
}

以相反的順序輸出乘法器,如

5! = 5 * 4 * 3 * 2 * 1 = 120

按以下方式重寫 for 循環

    for ( int i = n; i != 0; i-- )
    {
        fact *= i;

        if ( i != n ) printf( " * " );

        printf( "%d", i );
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM