• 【Educoder作业】C&C++结构实训


    【Educoder作业】C&C++结构实训

    学好结构体是学好对象的基础。

    T1 有理数化简

    知道结构体是干嘛的就能做了,注意一些地方的特判即可。

    #include 
    using namespace std;
    
    struct rationalNumber{
        int fenzi; // 分子
        int fenmu; // 分母
    };
    
    // 函数reduction:有理数化简,对传入的有理数n进行化简
    // 参数:n-有理数
    // 返回值:无化简后的有理数
    rationalNumber reduction(rationalNumber n);
    
    int main()
    {
        char c;
        rationalNumber x, y;
        cin >> x.fenzi >> c >> x.fenmu;   // 输入有理数,首先读入分子,然后是/,最后是分母
        y = reduction(x);   // 有理数化简
        // 输出化简的结果
        if(y.fenmu == 1)
            cout << y.fenzi << endl;
        else
            cout << y.fenzi << "/" << y.fenmu << endl;
        return 0;
    ,
    
    rationalNumber reduction(rationalNumber n)
    {
        // 请在这里补充代码,实现函数reduction
        /********** Begin *********/
    	int x = n.fenzi, y = n.fenmu;
    	if (!x) return (rationalNumber){x, 1};
    	int x_ = x < 0 ? -x : x;
    	for (int i = 2; i <= x_; i ++ ) {
    		while (x_ % i == 0 && y % i == 0) x_ /= i, y /= i;
    	}
    	x_ = x < 0 ? -x_ : x_;
    	return (rationalNumber){x_, y};
        
        
        /********** End **********/
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    T2 有理数加法

    没啥区别啊感觉,注意求 g c d gcd gcd别弄错了,实在不行咱就直接用 C + + C++ C++里自带的。

    #include 
    using namespace std;
    
    struct rationalNumber{
        int fenzi; // 分子
        int fenmu; // 分母
    };
    
    // 函数rnAdd:两个有理数相加
    // 参数:x,y-两个有理数
    // 返回值:x+y的最简分数形式
    rationalNumber rnAdd(rationalNumber x, rationalNumber y);
    
    int main()
    {
        char c;
        rationalNumber x, y, z;
        // 输入两个有理数
        cin >> x.fenzi >> c >> x.fenmu;
        cin >> y.fenzi >> c >> y.fenmu;
        z = rnAdd(x,y);     // 有理数相加
        // 输出相加的结果
        if(z.fenmu == 1)
            cout << z.fenzi << endl;
        else
            cout << z.fenzi << "/" << z.fenmu << endl;
        return 0;
    }
    
    // 请在此添加代码,实现函数rnAdd
    /********** Begin *********/
    rationalNumber rnAdd(rationalNumber x, rationalNumber y)
    {
    	rationalNumber z;
        z.fenzi = x.fenzi * y.fenmu + y.fenzi * x.fenmu;
        z.fenmu = x.fenmu * y.fenmu;
        int mdl = z.fenzi < 0 ? -z.fenzi : z.fenzi;
        for (int i = 2; i <= mdl; i ++ ) while (mdl % i == 0 && z.fenmu % i == 0) mdl /= i, z.fenmu /= i;
        if (!mdl) z.fenmu = 1;
        z.fenzi = mdl < 0 ? -mdl : mdl;
        return z;
    }
    /********** End **********/
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    T3 有理数平均数

    这个题我们处理好 g c d gcd gcd的同时,搞清楚分数乘即可,因为除法本质就是乘法。

    #include 
    using namespace std;
    
    struct rationalNumber{
        int fenzi; // 分子
        int fenmu; // 分母
    };
    
    // 函数rnMean:计算n个有理数的平均数
    // 参数:a-存放有理数的数组,n-有理数的个数
    // 返回值:n个有理数的平均数
    rationalNumber rnMean(rationalNumber a[], int n);
    
    int main()
    {
        char c;
        rationalNumber a[100],z;
        int n, i;
        cin >> n;     // 输入有理数个数
        // 输入n个有理数
        for(i = 0; i < n; i++)
            cin >> a[i].fenzi >> c >> a[i].fenmu;
        z = rnMean(a,n);     // 计算有理数平均数
        // 输出平均数
        if(z.fenmu == 1)
            cout << z.fenzi << endl;
        else
            cout << z.fenzi << "/" << z.fenmu << endl;
        return 0;
    }
    
    // 请在此添加代码,实现函数rnMean
    /********** Begin *********/
    rationalNumber disp(rationalNumber x) {
        for (int j = 2; j <= abs(x.fenzi); j ++ ) while (x.fenzi % j == 0 && x.fenmu % j == 0) x.fenzi /= j, x.fenmu /= j;
        return x;
    }
    rationalNumber rnMean(rationalNumber a[], int n)
    {
        if (n == 1) return disp(a[0]);
        // cout << a[0].fenzi << ' ' << a[0].fenmu << endl ;
    
        for (int i = 1; i < n; i ++ ) {
            a[0].fenzi = a[0].fenzi * a[i].fenmu + a[0].fenmu * a[i].fenzi, a[0].fenmu *= a[i].fenmu;
            a[0] = disp(a[0]);
        }
        a[0].fenmu *= n;
        a[0] = disp(a[0]);
        return a[0];
    }
    /********** End **********/
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    T4 书籍排序

    字典序的处理就是一个一个扫过去,如果会 s t r i n g string string的话就会很方便。不会的话注意读入,因为书名有空格所以不能用 s c a n f scanf scanf,但是 g e t l i n e getline getline会将上一个整数留在后面的回车读下来,从而不读书名。所以我们需要一个不记录的 g e t c h a r getchar getchar来满足我们的需求。

    #include 
    
    using namespace std;
    
    //请在此添加代码,实现书籍数据的输入、排序和输出
    /********** Begin *********/
    struct Node {
        char name[100];
    	double value;
    }a[100];
    
    int main()
    {
        int n; cin >> n ;
        for (int i = 1; i <= n; i ++ ) {
            char c = getchar();
            cin.getline(a[i].name, 100);
            cin >> a[i].value;
        }
        // for (int i = 1; i <= n; i ++ ) {
        //     cout << a[i].value << ", " << a[i].name << endl ;
        // }
        for (int i = 1; i < n; i ++ ) {
            int id = i;
            for (int j = i + 1; j <= n; j ++ ) {
                if (a[id].value > a[j].value) id = j;
                else if (a[id].value == a[j].value) {
                    int l1 = strlen(a[id].name), l2 = strlen(a[j].name);
                    bool flag = false;
                    for (int k = 0; k < min(l1, l2); k ++ ) {
                        if (a[j].name[k] < a[id].name[k]) {
                            flag = true;
                            break;
                        }
                        else if (a[j].name[k] > a[id].name[k]) {
                            break;
                        }
                    }
                    if (flag) id = j;
                }
            }
            swap(a[i], a[id]);
        }
        for (int i = 1; i <= n; i ++ ) {
            cout << a[i].value << ", " << a[i].name << endl ;
        }
        return 0;
    }
    /********** End **********/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    导致Spring事务失效的原因有哪些?
    小程序在政务服务平台建设中如何发挥价值
    Java Spring MVC如何编写一个示例程序呢?
    assertionError: Torch not compiled with CUDA enabled 无法使用gpu
    solidedge型材库/.sldlfp格式转.par
    WebSocket实现聊天功能
    【设计模式】Java设计模式 - 适配器模式
    [论文分享] Where’s Crypto?
    [攻防世界 XCTF 4th-WHCTF-2017] BABYRE
    开发 Chrome 扩展 之 Hello World 心血来潮
  • 原文地址:https://blog.csdn.net/JZYshuraK/article/details/128168257