#include <stdio.h> #include <stdlib.h> void test() { int i=0; int j=0; int k=0; int count=0; for(i=1;i<5;i++) { for(j=1;j<5;j++) { for(k=1;k<5;k++) { if((i!=j)&&(j!=k)&&(i!=k)) { count++; printf( "%d%d%d ",i,j,k); } } } } printf( "\nthe total=%d\n",count); } int main() { test(); system( "pause"); return 0; }
四年一閏,百年不閏,四百年再閏(即:判斷條件能被4整除但不能被100整除,或者能被400整除(year%4==0&&year%100!=0)||(year%400==0))
#include <stdio.h> #include <stdlib.h> void test() { int i=0; int count=0; for(i=1;i<2000;i++) { if((i%4==0&&i%100!=0)||(i%400==0)) { count++; printf( "%d ",i); } } printf( "\nthe total leap year before 2000 year=%d\n" ,count); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> void test() { float i=2; float j=1; float sum=0; float tmp=2; int count=0; //用於計數,前多少項 while(count<20) { sum+=i/j; count++; tmp=i; i=i+j; j=tmp; } printf( "sum=%f\n",sum); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> #include <math.h> void test() { int a=0; int n=0; int b=0; int s=0; int i=0; printf( "依次輸入a,n的值\n" ); scanf( "%d",&a); scanf( "%d",&n); b=a; for(i=1;i<=n;i++) { s+=a; a=b*pow(10.0,i)+a; } printf( "s=%d\n",s); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> void test() { double h=100; double s=0; for(int i=1;i<=10;i++) { s+=h*2; h=h/2; } s=s-100; //第一次多加了一個100 printf( "第10次落地時,共經過%f米,第10次反彈%f米高\n" ,s,h); } int main() { test(); system( "pause"); return 0; }