c語言求平面上2個坐標點的直線距離、求倆坐標直線距離作為半徑的圓的面積、遞歸、菲波那次數列、explode


 

  1 #include <stdio.h>
  2 #include <math.h>
  3 #include <string.h>
  4 
  5 char explode( char * str , char symbol );
  6 
  7 
  8 double distance ( int x1 , int y1 , int x2 , int y2 );                    // 求平面上2個坐標點的直線距離 
  9 double circle_area( double radius );                                    // 求圓面積。 radius 半徑 
 10 double two_point_cacl_circle_area ( int x1 , int y1 , int x2 , int y2 );// 從兩點坐標,求圓的面積
 11 
 12 // 階乘,遞歸方式實現。 
 13 int jiecheng( int N );
 14 
 15 // 遞歸方式,求2數的最大公約數 
 16 int digui_gongyueshu( int a , int b );
 17 // 菲波那次數列 
 18 int fibonacci( int N );
 19 
 20 int main( int argc , char ** argv ){
 21     char * str = "4;5;6;18;26;31;42;57;66;67;68;69;70;71;72;73;74;75;76;77;78;79;80;81;82;83;84;85;86;89;90;91;93;94;95;96;97;98;99;100;101;102;103;104;105;1051;1052;1310;1023;1041;1203;1256;1259;1260;1270;1210;1209;1279;1282;1278;1211;1276;1275;1240;1236;1235;1234;1239;1281;1028;1026;1231;1232;1277;1042;1050;1019;1267;1266;1268;1295;1265;1264;1258;1289;1219;1218;1217;1216;1016;1252;1251;1250;1249;1245;1244;1215;1243;1242;1302;1255;1287;1241;1253;1230;1271;1272;1054;1283;1284;1285;1286;";
 22     char * result = "";
 23     char list = {};
 24     int x1 = 3 , y1 = 7 , x2 = 7 , y2 = 9; // 2個坐標 
 25     
 26     printf( "\n求階乘的結果是:%d \n" , jiecheng( 4 ) );
 27         
 28     printf( "\n平面上2個坐標x1( %d , %d ) , x2(  %d , %d ),的直線距離是:%f \n" , x1 , y1 , x2 , y2 ,  distance( x1 , y1 , x2 , y2 ) );
 29     
 30     printf( "\n平面上2個坐標x1( %d , %d ) , x2(  %d , %d ),的直線距離作為圓的半徑時,這個圓的面積是:%f \n" , x1 , y1 , x2 , y2 ,  two_point_cacl_circle_area( x1 , y1 , x2 , y2 ) );
 31     
 32     
 33     printf( "\n 8和5 的最大公約數是: %d " , digui_gongyueshu(  8 , 5 ) );
 34     printf( "\n 8和15 的最大公約數是: %d \n" , digui_gongyueshu(  8 , 15 ) );
 35 
 36     int i = 0;
 37     for( ; i <= 8 ; ++i ){
 38         printf( "\n %d 的菲波那次數列 值是: %d " , i , fibonacci( i ) );
 39     } 
 40     
 41     //printf( "%s \n" , str );
 42     return 0;    
 43 }
 44 
 45 char explode( char * str , char symbol ){
 46     char list = {};
 47     int i = 0 , j = 0;
 48     int len = strlen( str ) ;
 49     // int len = sizeof( list ) / sizeof( int ); // 如果是int,float,double型, 通過sizeof()來計算list的長度
 50     
 51     /*
 52     for( ; i < len ; ++i ){
 53         if( str[ i ] != symbol ){
 54             list[ j ] += str[i];
 55         }
 56         else{
 57             ++ j;
 58         }
 59     }
 60     */
 61     
 62     return list;
 63 }
 64     
 65     
 66 
 67 // 通過畫勾股定理直角三角形 ,求平面上2個坐標點之間的巨鹿 
 68 double distance ( int x1 , int y1 , int x2 , int y2 )  {
 69     int x = 0 , y = 0;
 70     double res = 0.0;
 71     
 72     x = abs( x2 - x1 ); // 直角三角形的 勾 
 73     y = abs( y2 - y1 ); // 直角三角形的 股 
 74 
 75     res = sqrt( x * x + y * y ) ; // 勾股定理 求 斜線 
 76     return res;
 77 }
 78 
 79 // 求圓面積。 radius 半徑 
 80 double circle_area( double radius ){
 81     double pi = 3.1416 ;
 82     return pi * radius * radius;
 83 }
 84 
 85 // 從兩點坐標,求圓的面積
 86 double two_point_cacl_circle_area ( int x1 , int y1 , int x2 , int y2 ){
 87     return circle_area( distance( x1 , y1 , x2 ,  y2 ) );
 88 }
 89 
 90 // 階乘,遞歸方式實現。 
 91 int jiecheng( int N ){
 92     int res = 0 ;
 93     
 94     // 先寫1個,參數最小的情況的返回值 
 95     if( N == 0 ){
 96         res = 1;
 97     }
 98     // 再寫1個遞歸調用的情況。 
 99     else{
100         res = N * jiecheng( N - 1 );
101     }
102     printf( "%d階乘的結果:%d \n" , N , res );
103     // 完成遞歸。 
104     return res;
105     
106     /*
107         我們從數學上嚴格證明一下factorial函數的正確性。
108         剛才說了,factorial(n)的正確性依賴於factorial(n-1)的正確性,
109         只要后者正確,在后者的結果上乘個n返回這一步顯然也沒有疑問,那么我們的函數實現就是正確的。
110         
111         因此要證明factorial(n)的正確性就是要證明factorial(n-1)的正確性,
112         同理,要證明factorial(n-1)的正確性就是要證明factorial(n-2)的正確性,
113         
114         依此類推下去,最后是:要證明factorial(1)的正確性就是要證明factorial(0)的正確性。
115         
116         
117         而factorial(0)的正確性不依賴於別的函數,它就是程序中的一個小的分支return 1;,
118         這個1是我們根據階乘的定義寫的,肯定是正確的,因此factorial(1)也正確,
119         因此factorial(2)也正確,
120         依此類推,最后factorial(n)也是正確的。
121         
122         其實這就是中學時講的數學歸納法(Mathematical Induction),
123         用數學歸納法來證明只需要證明兩點:Base Case正確,遞推關系正確。        
124     */
125 }
126 
127 
128 // 遞歸方式,求2數的最大公約數 
129 int digui_gongyueshu( int a , int b ){
130     int res = 0 ;
131     if( a % b == 0 ){
132         res = b;
133     }
134     else{
135         res = digui_gongyueshu( b , a % b );
136     }
137     return res;
138     
139     /*
140         1、編寫遞歸函數求兩個正整數a和b的最大公約數(GCD,Greatest Common Divisor),使用Euclid算法:
141         
142           1. 如果a除以b能整除,則最大公約數是b。
143           2.否則,最大公約數等於b和a%b的最大公約數。
144         
145         Euclid算法是很容易證明的,請讀者自己證明一下為什么這么算就能算出最大公約數。    
146     */
147 }
148 
149 // 菲波那次數列 
150 int fibonacci( int N ){
151     int res = 0 ;
152     
153     if( N == 1 || N == 0 ){
154         res = 1;
155     }
156     else{
157         res = fibonacci( N - 1 ) + fibonacci( N - 2 );
158     }
159     return res;
160 }
161     
162     
View Code

 

 

c語言求平面上2個坐標點的直線距離、求倆坐標直線距離作為半徑的圓的面積、遞歸、菲波那次數列、explode


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM