2

这是我的代码,用于查找数字的阶乘并查找阶乘中所有数字的出现次数。

#include <stdio.h>
#include <stdbool.h>

int iFactorial(int iCount)
{
    int iProduct = 1;
    int iNumber = 1;
    while (iNumber <= iCount)
    {
        iProduct *= iNumber;
        iNumber++;
    }
    return iProduct;
}

int main(void)
{
    int iFac[10] = {0};
    int iCount = 0;
    printf("Please input a Integer: ");
    scanf("%d",&iCount);
    iFac[iCount] = iFactorial(iCount);
    printf("\nThe value of the factorial of %d is %d\n",iCount, iFac[iCount]);
    int i;
    int dig[10] = {0};
    while (iFac <=0)
    {
        int n;
        n= ((iFac % 10) + 1);
        dig[n] = dig[n] +1;
        iFac = iFac / 10;
    }

    for (i = 0; i > 9; i++)
    {
        if (dig[i+1] >0)
        {
            printf ("%d     %d\n", i, dig[i+1]);
        }
    }
}

我需要找到合适的写作方法array[x] = array[x] + 1

4

1 回答 1

1

我认为以下代码将清楚您想要如何计算数字。

#include<stdio.h>

int factorial (int n)
{
 if ( n == 1 ) return 1 ; 
 return n * factorial( n-1 ) ;
}

int main()
{

//Input number
int num ;
scanf( "%d", &num ) ;  

//Calculate Factorial
int fact = factorial ( num ) ;
cout<< "\nFactorial of Number is " << fact ;

//Count the frequency of Digits
int dig[10] = {0} ;

while( fact )
{
 int i = fact % 10 ;
 dig[i]++ ;
 fact /= 10 ;  
}

for ( int i = 0 ; i < 10 ; i++ )
   printf("\n The digit %d is present %d times " , i , dig[i] );

return 0 ;
}

确保您没有输入较大的值来计算数字的阶乘。

于 2012-11-17T10:39:25.687 回答