幾個C語言經典例題

來源:互聯網
上載者:User

一次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 );
}
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.