2.程序源代码:
main() { int n,t,number=20; float a=2,b=1,s=0; for(n=1;n<=number;n++) { s=s+a/b; t=a;a=a+b;b=t;/*这部分是程序的关键,请读者猜猜t的作用*/ } printf("sum is %9.6f\n",s); }
main() { float n,s=0,t=1; for(n=1;n<=20;n++) { t*=n; s+=t; } printf("1+2!+3!...+20!=%e\n",s); }
#include "stdio.h" main() { int i; int fact(); for(i=0;i<5;i++) printf("\40:%d!=%d\n",i,fact(i)); } int fact(j) int j; { int sum; if(j==0) sum=1; else sum=j*fact(j-1); return sum; }
age(n) int n; { int c; if(n==1) c=10; else c=age(n-1)+2; return(c); } main() { printf("%d",age(5)); }
main( ) { long a,b,c,d,e,x; scanf("%ld",&x); a=x/10000;/*分解出万位*/ b=x%10000/1000;/*分解出千位*/ c=x%1000/100;/*分解出百位*/ d=x%100/10;/*分解出十位*/ e=x%10;/*分解出个位*/ if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a); else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b); else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c); else if (d!=0) printf("there are 2, %ld %ld\n",e,d); else if (e!=0) printf(" there are 1,%ld\n",e); }
main( ) { long ge,shi,qian,wan,x; scanf("%ld",&x); wan=x/10000; qian=x%10000/1000; shi=x%100/10; ge=x%10; if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/ printf("this number is a huiwen\n"); else printf("this number is not a huiwen\n"); }
#define N 10 main() {int i,j,min,tem,a[N]; /*input data*/ printf("please input ten num:\n"); for(i=0;i<N;i++) { printf("a[%d]=",i); scanf("%d",&a[i]);} printf("\n"); for(i=0;i<N;i++) printf("%5d",a[i]); printf("\n"); /*sort ten num*/ for(i=0;i<N-1;i++) {min=i; for(j=i+1;j<N;j++) if(a[min]>a[j]) min=j; tem=a[i]; a[i]=a[min]; a[min]=tem; } /*output data*/ printf("After sorted \n"); for(i=0;i<N;i++) printf("%5d",a[i]); }
main() { int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j; printf("original array is:\n"); for(i=0;i<10;i++) printf("%5d",a[i]); printf("\n"); printf("insert a new number:"); scanf("%d",&number); end=a[9]; if(number>end) a[10]=number; else {for(i=0;i<10;i++) { if(a[i]>number) {temp1=a[i]; a[i]=number; for(j=i+1;j<11;j++) {temp2=a[j]; a[j]=temp1; temp1=temp2; } break; } } } for(i=0;i<11;i++) printf("%6d",a[i]); }
#define N 5 main() { int a[N]={9,6,5,4,1},i,temp; printf("\n original array:\n"); for(i=0;i<N;i++) printf("%4d",a[i]); for(i=0;i<N/2;i++) {temp=a[i]; a[i]=a[N-i-1]; a[N-i-1]=temp; } printf("\n sorted array:\n"); for(i=0;i<N;i++) printf("%4d",a[i]); }
main() { int i,j; int a[10][10]; printf("\n"); for(i=0;i<10;i++) { a[i][0]=1; a[i][i]=1; } for(i=2;i<10;i++) for(j=1;j<i;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;i<10;i++) { for(j=0;j<=i;j++) printf("%5d",a[i][j]); printf("\n"); } }
[算法]有趣算法合辑[21-30],布布扣,bubuko.com
原文地址:http://blog.csdn.net/wzhiu/article/details/31399747