學堂在線TsinghuaX: 00740043X C++語言程序設計基礎 第四章Lab


第一題:最大公約數

題目描述

求兩個正整數a 和 b的最大公約數。

要求使用c++ class編寫程序。可以創建如下class

#include <iostream>
using namespace std;
class Integer {
private:
    int _num;
public:
//構造函數
    Integer(int num) {
    }
//計算當前Integer 和 b之間的最大公約數
    int gcd(Integer b) {
    }
};
int main(){
    int a, b;
    cin >> a >> b;
    Integer A(a);
    Integer B(b);
    cout << A.gcd(B) << endl;
    return 0;
}

 

輸入描述

兩個正整數a,b,並且1=<a,b <=10000

輸出描述

a和b的最大公約數

樣例輸入

1000 1000

樣例輸出

1000
#include <iostream>
using namespace std;
class Integer {
private:
    int _num;
public:
//構造函數
    Integer(int num) {
        _num=num;
    }
//計算當前Integer 和 b之間的最大公約數
    int gcd(Integer b) {
        int c=b._num,d;
        _num>c?d=c:d=_num;
        while((_num%d!=0)||(c%d!=0))
                d--;
         return d;
    }
};
int main(){
    int a, b;
    cin >> a >> b;
    Integer A(a);
    Integer B(b);
    cout << A.gcd(B) << endl;
    return 0;
}

第二題:反轉整數

題目描述

對於輸入的一個正整數,輸出其反轉形式 

要求使用c++ class編寫程序。可以創建如下class

#include <iostream>
using namespace std;
 
class Integer{
private:
    int _num;
//getLength()函數獲取_num長度
    int getLength(){
    }
public:
//Integer類構造函數
    Integer(int num){
    }
//反轉_num
    int inversed(){
    }
};
 
int main() {
    int n;
    cin >> n;
    Integer integer(n);
    cout << integer.inversed() << endl;
}

輸入描述

一個正整數a ,且1=<a<=1,000,000,000

輸出描述

a的反轉形式

樣例輸入

1011

樣例輸出

1101
#include <iostream>
using namespace std;
class Integer{
private:
    int _num;
//getLength()函數獲取_num長度
    int getLength(){
         int x=_num;
         int len=0;
         while(x!=0||x%10!=0)
         {
              len++;
              x/=10;
         }
         return len;
    }
public:
//Integer類構造函數
    Integer(int num){
         _num=num;
    }
//反轉_num
    int inversed(){
         int len = getLength();
         int tmp=_num;
         int s=0;
         for(int i=0;i<len;i++)
         {
              s=s*10+tmp%10;
              tmp/=10;
         }
         return s;
    }
};
 
int main() {
    int n;
    cin >> n;
    Integer integer(n);
    cout << integer.inversed() << endl;
}

第三題:一元二次方程求解

題目描述

對於一元二次方程ax^2 + bx + c = 0,解可以分為很多情況。

若該方程有兩個不相等實根,首先輸出1,換行,然后從小到大輸出兩個實根,換行;

若該方程有兩個相等實根,首先輸出2,換行,然后輸出這個這個實根,換行;

若該方程有一對共軛復根,輸出3,換行;

若該方程有無解,輸出4,換行;

若該方程有無窮個解,輸出5,換行;

若該方程只有一個根,首先輸出6,換行,然后輸出這個跟,換行;

要求使用c++ class編寫程序。可以創建如下class

#include <iostream>
#include <cmath>
using namespace std;
class Equation{
private:
    int _a, _b, _c;
public:
    Equation(int a, int b, int c){
    }
    void solve(){
    }
};
int main(){
    int a, b, c;
    cin >> a >> b >> c;
    Equation tmp(a, b, c);
    tmp.solve();
}

輸入描述

該一元二次方程的系數a,b,c,且-100=<a,b,c<=100

輸出描述

解的情況。輸出解的時候保留兩位小數

樣例輸入

1 4 3

樣例輸出

1
-3.00 -1.00
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
class Equation {
private:
    int _a, _b, _c, _x;
public:
    Equation(int a, int b, int c) {
        _a = a, _b = b, _c = c;
        _x = _b * _b - 4 * _a * _c;
    }
    void solve() {
        if (_x > 0 && _a != 0)
        {
            cout << 1 << endl;
            double x = -_b / 2.0 / (double)_a;
            double y = sqrt(_x) / 2.0 / (double)_a;
            printf("%.2f %.2f\n", x - y, x + y);
        }
        else if(_x==0&&_a!=0)
        {
            cout << 2 << endl;
            double x = -_b / 2.0 / (double)_a;
            printf("%.2f\n", x);
        }
        else if(_x<0&&_a!=0)
        {
            cout<<3<<endl;
        }
        else if(_a==0&&_b==0&&_c!=0)
        {
            cout<<4<<endl;
        }
        else if(_a==0&&_b==0&&_c==0)
        {
            cout<<5<<endl;
        }
        else if(_a==0&&_b!=0)
        {
            cout<<6<<endl;
            double x = -_c /(double)_b;
            printf("%.2f\n", x);
        }
    }
};

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    Equation tmp(a, b, c);
    tmp.solve();
}

 


免責聲明!

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



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