#include <stdio.h> #include <stdlib.h> void test() { int i=0; int sum=1; for(i=9;i>0;i--) { sum=(sum+1)*2; } printf( "the total=%d\n",sum); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> #include <math.h> void test() { int i=0; float sum=0; for(;i<100;i++) { sum+=(pow(-1.0,i))/(i+1); } printf( "sum=%f\n",sum); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> void test() { int tmp=1; int sum=0; for(int i=1;i<=20;i++) { tmp=i*tmp; sum+=tmp; } printf( "sum=%d\n",sum); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> void test() { int m=0; printf( "Please input the figure:" ); scanf( "%d",&m); int n=0; //反轉後的數 int tmp=m; while(tmp) { n=n*10+tmp%10; tmp/=10; } if(n==m) printf( "Yes\n"); else printf( "No\n"); } int main() { test(); system( "pause"); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> void test() { char* str="12bn21" ; char* start=str; char* end=str+strlen(str)-1; //注意此處end的定義 while(start<end) { if(*start==*end) { start++; end--; } else { printf( "%s\n",str); printf( "No\n"); return; } } printf( "%s\n",str); printf( "Yes\n"); } int main() { test(); system( "pause"); return 0; }
改爲char* str="123abnba321" ; 結果爲
sizeof:運算符,計算字節數
strlen:函數,計算字符串長度