几个C语言经典例题

来源:百度文库 编辑:神马文学网 时间:2024/06/05 00:06:32
一次for循环完成1!+2!+...+10!.c
main()
{
long s=0,n=1;
int i;
for(i=1;i<=10;i++)
{
n=n*i;
s=s+n;
}
printf("s=%ld\n",s);
}
杨辉三角.c
main()
{
int a[10][10], x, y;
for(x=0;x<10;x++)
for(y=0;y<10;y++)
a[x][y]=0;
for(x=0;x<10;x++)
a[x][0]=1;
for(x=1;x<10;x++)
for(y=1;y<10;y++)
{
a[x][y]=a[x-1][y-1]+a[x-1][y];
if(a[x][y]==1)
break;
}
for(x=0;x<10;x++)
{
for(y=0;y<10;y++)
{
if(a[x][y]!=0)
printf("%d",a[x][y]);
}
printf("\n");
}
}
连接两字符串.c
#include "stdio.h"
main()
{
int i,j=0;
char str1[50], str2[20];
scanf("%s%s",str1,str2);
for(i=0;str1[i]!='\0';i++);
for(;;i++,j++)
{
str1[i]=str2[j];
if(str1[i]=='\0')
break;
}
}
找闰年.c
main()
{
int a;
scanf("%d",&a);
if(    (   (a%4==0) && (a%100!=0)   ) || (a%400==0)    )
printf("闰年\n");
}
找水仙花数.c
main()
{
int g, s, b, x;
for(x=100;x<1000;x++)
{
g=x%10;
s=x/10%10;
b=x/100;
if( g*g*g + s*s*s + b*b*b == x )
printf("%d\t",x);
}
}
百钱买百鸡.c
main()
{
int x, y, z;
for(x=0;x<=20;x++)
{
for(y=0;y<=34;y++)
{
z = 100 - (x+y) ;
if ( 5*x + 3*y + z/3 == 100 )
printf( "\t公鸡%d只\t母鸡%d只\t小鸡%d只\n", x, y, z );
}
}
}