---
# System prepended metadata

title: codape - 作业

---

# codape - 作业

## HCF
```cpp=

#include<iostream>

using namespace std; 

int hcf(int a, int b)
{

if(b==0) return a;

return hcf(b ,a % b);
}

int main() {

int x, y;

while(cin >> x >> y){

if(x >= 1 && x <= 10000 && y >= 1 && y <= 10000){

    cout << hcf(x,y);
    
        }
    }
}
```
原理：用欧几里得算法（辗转相除法）



## Fibonacci
```cpp=
#include <iostream>
using namespace std;

int F(int n ){
  int t0 = 0, t1 = 1;

  if(n == 1) return 0;

  n -= 2;

  while(n--){
    int next = t0 + t1;
    t0 = t1;
    t1 = next;
  }
  return t1 ;
}

int main() {
  int a;
  while(cin >> a) {cout << F(a);}
 
}

```
原理：用to t1 next 来暂存前两个数字 用while来找到数列的第n个数字



## Quadratic Equation
```cpp=
#include <iostream>
#include <cmath>
using namespace std;

void printSol(int a , int b , int c){
  double cal = b*b - 4*a*c;
  double x1 = (-b + sqrt(cal)) / (2*a);
  double x2 = (-b - sqrt(cal)) / (2*a);
  if(x1 != x2){
    printf("x1 = %.2f\n x2 = %.2f",x1,x2);
  }
  else printf("x = %.2f\n",x1);
}

bool root(int a ,int b , int c){
  int cal = b*b - 4*a*c;
  if(cal >= 0) return true;
  else return false;
}

int main() {
 int a , b, c;
 while(cin >> a >> b >> c){
   if(root(a,b,c)) printSol(a,b,c);
   else cout << "No Real Solution\n";
 }
}
```
用root（boolean）来作为判别式，判定是否有实数根。再用printSol来输出解