6-1 简单输出整数
void PrintN(int N)
{
int i;
for(i=1;i<=N;i++)
printf("%d\n",i);
}
6-2 多项式求值
double f(int n,double a[],double x)
{
if (n==0)
return a[0];
int i;
float sum=a[n]*x+a[n-1];
for(i=n-1;i>=1;i--)
{
sum=sum*x+a[i-1];
}
return sum;
}
6-3 简单求和
int Sum(int List[],int N)
{
if(N==1)
return List[0];
return Sum(List+1,N-1)+List[0];
}
6-4 求自定类型元素的平均
ElementType Average(ElementType S[],int N)
{
int i;
ElementType sum=0;
for(i=0;i<N;i++)
{
sum+=S[i];
}
return sum/N;
}
6-5 求自定类型元素的最大值
ElementType Max( ElementType S[], int N )
{
int index=0;
for(int i=1;i<N;i++)
{
if(S[i]>S[index])
index=i;
}
return S[index];
}
6-6 求单链表结点的阶乘和
int FactorialSum( List L )
{
if(L==NULL)
return 0;
int factorial=1;
for(int i=1;i<=L->Data;i++)
{
factorial*=i;
}
return factorial+FactorialSum(L->Next);
}
6-7 统计某类完全平方数
int IsTheNumber(const int N)
{
int res=sqrt(N);
int array[10]={0};
if(res*res==N)
{
int rest=N;
while(rest)
{
array[rest%10]++;
if(array[rest%10]>=2)
return 1;
rest/=10;
}
}
return 0;
}
6-8 简单阶乘计算
int Factorial( const int N )
{
if(N<0||N>12)
return 0;
if(N<=1)
return 1;
return N*Factorial(N-1);
}
6-9 统计个位数字
int Count_Digit ( const int N, const int D )
{
if(N==D)
return 1;
int rest=N>0?N:-N;
int count=0;
while(rest)
{
if(D==rest%10)
count++;
rest/=10;
}
return count;
}
6-10 阶乘计算升级版
#define SIZE 10000
int multiple(int *lhs, int rhs,int high)
{
// Init the multiple params array.
int mArrays[4],i=0,j;
for (; i < 4; i++)
{
mArrays[i] = -1;
}
i = 0;
while (rhs)
{
mArrays[i++] = rhs % 10;
rhs /= 10;
}
// Store the size of mArrays
int sizeMArrays = i;
// Init the res array (store the res.
int resArray[SIZE];
for (i = 0; i < SIZE; i++)
resArray[i] = 0;
// Do Multiple
for (i = 0; i < sizeMArrays; i++)
{
for (j = 0; j < high; j++)
{
int res = mArrays[i] * lhs[j]+resArray[i+j];
resArray[j + i] = res % 10;
if (res >= 10)
{
resArray[j + i + 1] += res / 10;
}
}
}
high += sizeMArrays - 1;
if (resArray[high])
{
high++;
}
for (i = 0; i < high; i++)
lhs[i] = resArray[i];
return high;
}
void Print_Factorial ( const int N )
{
if(N<0||N>1000)
{
printf("Invalid input\n");
return;
}
else if (N == 0)
{
printf("1\n");
return;
}
int res[SIZE];
int high = 1;
res[0] = 1;
int i,j;
for (i = 1; i <= N; i++)
{
high=multiple(res, i, high);
}
for (int i = high - 1; i >= 0; i--)
printf("%d", res[i]);
}
记录一下,每天上班摸鱼写写算法