总结了C语言一些经典的算法,希望能够帮助你更好的学习C语言
南航09-10年C语言常用算法总结
一、第五章常用算法总结
(1)分段函数的计算
/***** P8 5.20 *****/
main( )
{
int x,y;
scanf("%d",&x);
if ( x>2 && x<=10 ) y=x*(x+2);
else if ( x>-1 && x<=2 ) y=2*x;
else if (x<=-1) y=x-1;
else y=-1;
printf(“%d %d \n”,x, y );
}
/* P37 实验四 1 */
# include
main( )
{
int x,y;
scanf("%d",&x);
if (x<1) y=x;
else if ( x<10 ) y=2*x-1;
else y=3*x-11;
printf("%d %d \n",x,y);
}
二、第六章常用算法总结
(1)多项式累加和、累乘积;
/* P11 6.8 */
# include
main()
{
int a,b,c,i;
a=c=0;
for ( i=0;i<=10;i=i+2)
{
a+=i;
b=i+1;
c+=b;
}
printf("sum of the even=%d\n",a);
printf("sum of the odd =%d\n",c-11);
}
实验五 2 求1!+2!+3!+4!+5!+…
main()