Leetcode 226 翻轉二叉樹
阿新 • • 發佈:2020-12-03
//一元二次方程求解 //重複執行, 直到按下Ctrl+Z結束 #include<stdio.h> #include<math.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!= %11d\n", i, fac(i)); return 0; } //函式定義 long long fac(int n){ static long long p = 1; printf("p = %11d\n", p); p = p*n; return p; }
//練習:區域性static變數特性 #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); }
一致,8,17
儲存呼叫函式的值的改變
//尋找兩個整數之間的所有素數(包括這兩個整數),把結果儲存在陣列bb中,函式返回素數的個數。 //例如,輸入6和21,則輸出為:7 11 13 17 19。 #include<stdio.h> #define N 1000 int fun(int n,int m,int bb[N]){ int i,j,k=0,flag; for(j=n;j<=m;j++){ flag=1; for(i=2;i<j;i++) if(j%i==0){ flag=0; break; } if(flag!=0 ) bb[k++]=j; } return k; } int main(){ int n=0,m=0,i,k,bb[N]; scanf("%d",&n); scanf("%d",&m); for(i=0;i<m-n;i++) bb[i]=0; k=fun(n,m,bb); for(i=0;i<k;i++) printf("%4d",bb[i]); return 0; }
#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 = %11d\n",n,f); } return 0; } long long fun(int n){ long long a; if(n==0) a=0; else a=2*fun(n-1)+1; return a; }
//一元二次方程求解 //重複執行, 直到按下Ctrl+Z結束 #include<stdio.h> #include<math.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); } } }
不對