4. 列表一學完,Python 會一半,滾雪球學 Python
阿新 • • 發佈:2020-11-28
// 一元二次方程求解 (函式實現方式) // 重複執行, 直到按Ctrl+Z結束 #include <math.h> #include <stdio.h> // 函式宣告 void solve(double a, double b, double c); // 主函式 int main() { double a, b, c; printf("Enter a, b, c: "); while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { solve(a, b, c);// 函式呼叫 printf("Enter a, b, c: "); } return 0; } // 函式定義 // 功能:求解一元二次方程,列印輸出結果 // 形式引數:a,b,c為一元二次方程係數 void solve(double a, double b, double c) { double x1, x2; double delta, real, imag; if(a == 0) printf("not quadratic equation.\n"); else { delta = b*b - 4*a*c; if(delta >= 0) { x1 = (-b + sqrt(delta)) / (2*a); x2 = (-b - sqrt(delta)) / (2*a); printf("x1 = %f, x2 = %f\n", x1, x2); } else { real = -b/(2*a); imag = sqrt(-delta) / (2*a); printf("x1 = %f + %fi, x2 = %f - %fi\n", real, imag, real, imag); } } }
// 利用區域性static變數計算階乘 #include <stdio.h> long long fac(int n); // 函式宣告 int main() { int i,n; printf("Enter n: "); scanf("%d", &n); for(i=1; i<=n; ++i) printf("%d! = %lld\n", i, fac(i)); return 0; } // 函式定義 long long fac(int n) { static long long p = 1; printf("p=%lld\n",p); p = p*n; return p; }
#include<stdio.h> int func(int, int); int main() { int k=4,m=1,p1,p2; p1 = func(k,m) ; p2 = func(k,m) ; printf("%d,%d\n",p1,p2) ; return 0; } int func(int a,int b) { static int m=0,i=2; i += m+1; m = i+a+b; return (m); }
#include<stdio.h> int func(int, int); int main() { int k=4,m=1,p1,p2; p1 = func(k,m) ; p2 = func(k,m) ; printf("%d,%d\n",p1,p2) ; return 0; } int func(int a,int b) { static int m=0,i=2; i += m+1; m = i+a+b; return (m); }
#include <stdio.h> long long fun(int n); // 函式宣告 int main() { int n; long long f; while(scanf("%d", &n) != EOF) { f = fun(n); // 函式呼叫 printf("n = %d, f = %lld\n", n, f); } return 0; } // 函式定義 long long fun(int n) { long long int p; if (n==1) p==1; else p=fun(n-1)*2+1; return p; }
#include <stdio.h> void draw(int n, char symbol); // 函式宣告 #include <stdio.h> int main() { int n, symbol; while(scanf("%d %c", &n, &symbol) != EOF) { draw(n, symbol); // 函式呼叫 printf("\n"); } return 0; } // 函式定義 void draw(int n,char symbol){ int i,a,b,c,d; a=n; for(i=1;i<=n;i++) { d=2*i-1; for(b=1;b<=(a-i);b) { printf(""); } for(c=1;c<=d;c++){ printf("%c",symbol); } printf("\n"); } for (i=1;i<=n;i++) { d=2*i-1; for(b=1;b<=(a-i);b++) { printf(""); } for(c=1;c<=d;c++){ printf("%c",symbol); } printf("\n"); } }