• 东北大学acm暑期夏令营结构体


    NEUQ-ACM-CAMP-B011-结构体-枚举
    NEUQ-ACM-CAMP-B011-结构体-枚举
    开始时间 2022/08/18 08:07:00
    结束时间 2022/08/31 23:59:00
    答题时长 19672分钟
    答卷类型 标准答案
    总分 100

    程序填空题 得分:暂无 总分:10
    5-1

    下列程序读入时间数值,将其加1秒后输出,时间格式为:hh: mm: ss,即“小时:分钟:秒”,当小时等于24小时,置为0。

    #include 
    struct { 
         int hour, minute, second;
    } time;
    int main(void)
    {  
          scanf("%d:%d:%d", (1分) );
          time.second++;
          if( (1分) == 60){
               (1分) ; 
              time.second = 0;
              if(time.minute == 60){
                  time.hour++; 
                  time.minute = 0;
                  if( (1分)  ) 
                     time.hour = 0; 
              }
          }
          printf ("%d:%d:%d\n", time.hour, time.minute, time.second );
    
      return 0;
    
    • 1
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    }


    5-2

    读入一个整型数表示的秒数,将它转换为时分秒格式并输出。

    #include 
    struct TIME {
        int sec, min, hour;
    } time;
    
    • 1
    • 2
    • 3
    • 4

    int main()
    {
    int t;

    scanf("%d", &t);
    time.hour = (2分);
    time.min = (2分);
    time.sec = t % 60;
    
    printf ("(2分)\n", time.hour, time.min, time.sec);
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    }

    输入样例

    4508
    
    • 1

    输出样例

    01:15:08
    
    • 1

    函数题 得分:15 总分:15
    6-1
    计算两个复数之积
    (10分)

    本题要求实现一个计算复数之积的简单函数。

    函数接口定义:

    struct complex multiply(struct complex x, struct complex y);
    
    • 1

    其中struct complex是复数结构体,其定义如下:

    struct complex{
        int real;
        int imag;
    };
    
    • 1
    • 2
    • 3
    • 4

    裁判测试程序样例:

    #include 
    
    • 1

    struct complex{
    int real;
    int imag;
    };

    struct complex multiply(struct complex x, struct complex y);

    int main()
    {
    struct complex product, x, y;

    scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
    product = multiply(x, y);
    printf("(%d+%di) * (%d+%di) = %d + %di\n", 
            x.real, x.imag, y.real, y.imag, product.real, product.imag);
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    /* 你的代码将被嵌在这里 */

    输入样例:

    3 4 5 6
    
    • 1

    输出样例:

    (3+4i) * (5+6i) = -9 + 38i
    
    • 1
    编译器
    GCC
    代码
    struct complex multiply(struct complex x, struct complex y)
    {
        int real, imag;
        real = x.real*y.real - x.imag*y.imag;
        imag = x.imag*y.real + x.real*y.imag;
        struct complex ret={real,imag};
        return ret;
    };
    
    评测结果
    答案正确 (10 分)
    编译器输出
    a.c: In function ‘main’:
    a.c:14:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    测试点得分
    测试点结果得分耗时内存
    0答案正确52.00 ms188 KB
    1答案正确32.00 ms372 KB
    2答案正确22.00 ms196 KB

    6-2
    空间两点间的距离
    (5分)

    已知空间中两点 A(x1,y1,z1)A(x_1,y_1, z_1)A(x1,y1,z1)B(x2,y2,z2)B(x_2,y_2, z_2)B(x2,y2,z2) 之间的欧氏距离定义为:(x1−x2)2+(y1−y2)2+(z1−z2)2\sqrt{(x_1-x_2)^2+(y_1-y_2)^2+(z_1-z_2)^2}(x1x2)2+(y1y2)2+(z1z2)2

    本题要求实现一个函数,计算平面上两点之间的欧氏距离,平面上点的坐标通过以下结构给出:

    struct point {
        double x, y, z;
    };
    
    • 1
    • 2
    • 3

    函数接口定义:

    //  计算并返回平面上两点 a 和 b 之间的欧氏距离
    double distance(struct point a, struct point b);
    
    • 1
    • 2

    裁判测试程序样例:

    #include 
    #include 
    
    • 1
    • 2

    struct point {
    double x, y, z;
    };

    void read_point(struct point *p);
    double distance(struct point a, struct point b);

    int main(void)
    {
    struct point p1, p2;

    read_point(&p1);
    read_point(&p2);
    
    printf("%f\n", distance(p1, p2));
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    void read_point(struct point *p)
    {
    scanf("%lf %lf %lf", &p->x, &p->y, &p->z);
    }

    /* 请在这里填写答案 */

    输入样例:

    在这里给出一组输入。例如:

    0 0 0 3 0 4
    
    • 1

    输出样例:

    在这里给出相应的输出。例如:

    5.000000
    
    • 1
    编译器
    GCC
    代码
    //  计算并返回平面上两点 a 和 b 之间的欧氏距离
    double distance(struct point a, struct point b)
    {
        double dis;
        dis = sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)+pow(a.z-b.z,2));
        return dis;
    }
    评测结果
    答案正确 (5 分)
    编译器输出
    a.c: In function ‘read_point’:
    a.c:25:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lf %lf %lf", &p->x, &p->y, &p->z);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    测试点得分
    测试点结果得分耗时内存
    sample1答案正确32.00 ms188 KB
    sample2答案正确22.00 ms328 KB

    编程题 得分:75 总分:75
    7-1
    复数运算
    (5分)

    复数是由两个实数分别作为实部和虚部构成的一个复合数,从另一个角度来说复数就是由两个实数构成的有序对,在C语言中适合用结构类型来表示复数。现在要求用结构类型

    typedef struct
    {
        float x;
        float y;
    } Comp;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    及其变量来表示与存储复数,编写程序实现复数的加减法运算。

    输入格式:

    在一行输入四个用空格分开的实数a1 b1 a2 b2分别表示复数c1 = a1 + b1ic2 = a2 + b2i

    输出格式:

    复数的输出应符合数学上关于复数的表示习惯:实部与虚部都为零时只输出一个0.00; 有一个为零时,只输出非零的部分; 虚部为负时,例如3-4i,应输出为3.00-4.00i的形式,不要输出为3.00+-4.00i。实部与虚部均保留2位小数,例如3.00-4.00i
    输出在两行进行,第一行输出求和的结果,第二行输出求差的结果。

    输入样例:

    5.00 4.00 3.00 2.00
    
    • 1

    输出样例:

    8.00+6.00i
    2.00+2.00i
    
    • 1
    • 2
    编译器
    GCC
    代码
    #include
    #include
    typedef struct
    {
        float x;
        float y;
    } Comp;
    

    Comp add(Comp x, Comp y);
    Comp sub(Comp x, Comp y);
    Comp multiply(Comp x, Comp y);
    void print(Comp x);

    int main()
    {
    float a1,b1,a2,b2;
    scanf("%f%f%f%f",&a1,&b1,&a2,&b2);

    Comp x,y;
    x.x=a1, x.y=b1;
    y.x=a2, y.y=b2;
    
    Comp a = add(x,y);
    Comp b = sub(x,y);
    
    print(a);
    printf("\n");
    print(b);
    
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    }

    Comp multiply(Comp x, Comp y)
    {
    float real, imag;
    real = x.xy.x - x.yy.y;
    imag = x.yy.x + x.xy.y;
    Comp ret={real,imag};
    return ret;
    };

    Comp add(Comp x, Comp y)
    {
    float real, imag;
    real = x.x+y.x;
    imag = x.y+y.y;
    Comp ret;
    ret.x=real,ret.y = imag;
    return ret;
    };

    Comp sub(Comp x, Comp y)
    {
    float real, imag;
    real = x.x-y.x;
    imag = x.y-y.y;
    Comp ret;
    ret.x=real,ret.y = imag;
    return ret;
    };

    void print(Comp x)
    {
    if (x.x0 && x.y0)
    printf(“0.00”);
    else if(x.y==0)
    printf(“%.2f”,x.x);
    else if(x.x == 0)
    printf(“%.2fi”,x.y);
    else if(x.y>0)
    printf(“%.2f+%.2fi”,x.x,x.y);
    else
    printf(“%.2f%.2fi”,x.x,x.y);
    }

    评测结果
    答案正确 (5 分)
    编译器输出
    a.c: In function ‘main’:
    a.c:19:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
    scanf(“%f%f%f%f”,&a1,&b1,&a2,&b2);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    测试点得分
    测试点结果得分耗时内存
    1答案正确22.00 ms320 KB
    2答案正确12.00 ms188 KB
    3答案正确12.00 ms184 KB
    4答案正确12.00 ms188 KB

    7-2
    结构体应用:计算总分及最高分
    (10分)

    本题目要求先输入正整数N,然后输入N个类型为结构体stud的数组元素,计算每个学生的总分,输出每个学生的学号、姓名、三门课的成绩及总分;计算全部成绩的平均分并输出;输出总分最高同学的各项信息。
    struct stud {
    int num; //学号
    char name[10]; //姓名
    int score[3]; //3门课成绩
    int sum; //总分
    };

    输入格式:

    先输入不超过10的一个正整数N,然后每行输入一个学生的信息(学号、姓名、三门课成绩)。学号在整数范围内,姓名长度小于10个字符。

    输出格式:

    首先输出每个学生的信息(包括学号、姓名、三门课成绩、总分),数据项之间空1格,每人一行;再输出全部成绩的平均分;最后输出总分最高(假设没有相同总分)同学的学号、姓名、三门课成绩及总分,数据项之间空1格。

    输入样例:

    在这里给出一组输入。例如:

    4
    1  张三  81  85  82
    2  李四  82  78  74
    3  王五  85  74  90
    4  赵六  77  85  79
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出样例:

    在这里给出相应的输出。例如:

    1 张三 81 85 82 248
    2 李四 82 78 74 234
    3 王五 85 74 90 249
    4 赵六 77 85 79 241
    总平均分=81.000000
    3 王五 85 74 90 249
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    编译器
    GXX
    代码
    #include 
    using namespace std;
    struct stud
    {
        int num; //学号
        char name[10]; //姓名
        int score[3]; //3门课成绩
        int sum; //总分
    };
    

    int main()
    {
    int n;
    cin>>n;

    stud st[n];
    int max=-1, pos=-1,sum=0;
    for(int i=0;i<n; i++)
    {
        cin>>st[i].num;
        cin>>st[i].name;
        cin>>st[i].score[0]>>st[i].score[1]>>st[i].score[2];
        st[i].sum=st[i].score[0]+st[i].score[1]+st[i].score[2];
        if (max < st[i].sum)
        {
            max = st[i].sum;
            pos = i;
        }
        sum += st[i].sum;
    }
    
     for(int i=0;i<n; i++)
     {
         cout<<st[i].num<<' '<<st[i].name<<' '
             <<st[i].score[0]<<' '
             <<st[i].score[1]<<' '
             <<st[i].score[2]<<' '
             <<st[i].sum<<endl;
     }
    double avg = (double)sum / (n*3.0);
    cout<<"总平均分=";
    printf("%.6f\n",avg);
    
    int i=pos;
    cout<<st[i].num<<' '<<st[i].name<<' '
             <<st[i].score[0]<<' '
             <<st[i].score[1]<<' '
             <<st[i].score[2]<<' '
             <<st[i].sum<<endl;
    
    return 0;
    
    • 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

    }

    评测结果
    答案正确 (10 分)
    测试点得分
    测试点结果得分耗时内存
    0答案正确53.00 ms448 KB
    1答案正确33.00 ms444 KB
    2答案正确23.00 ms448 KB

    7-3
    2021-结构体-circle
    (5分)

    定义一个结构体类型表示一个圆(x,y,r),圆心坐标是(x,y),圆半径是r。从键盘输入一个圆的圆心坐标和半径,坐标值和半径均为整型数据,输出这个圆的面积,面积为float。面积公式为area=3.14∗r∗rarea =3.14rrarea=3.14rr.

    输入格式:

    从键盘输入圆的圆心坐标和半径,之间用空格分隔

    输出格式:

    输出数据保留两位小数。

    输入样例:

    在这里给出一组输入。例如:

    3 4 5
    
    • 1

    输出样例:

    在这里给出相应的输出。例如:

    78.50
    
    • 1
    编译器
    GXX
    代码
    #include 
    using namespace std;
    struct Circle
    {
        int x,y,r;
        float GetArea()
        {
            return (float) 3.14 * r * r;
        }
    };
    

    int main()
    {
    Circle c1;
    cin>>c1.x>>c1.y>>c1.r;

    cout<<fixed<<setprecision(2)<< c1.GetArea();
    
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4

    }

    评测结果
    答案正确 (5 分)
    测试点得分
    测试点结果得分耗时内存
    1答案正确53.00 ms444 KB

    7-4
    寻找最高分
    (5分)

      给定n(n⩾1)n(n\geqslant1)n(n1)个学生的学号、姓名和3门课的考试成绩。编写程序找出总分最高的学生,并输出其学号、姓名和总分。如果有多个相同的最高分,则输出所有最高分学生的信息。
    要求:
      存储学生信息及考试成绩的变量用如下结构类型来定义。

    struct  Student
    {
        char num[11];        //学号,最多10个字符
        char name[11];       //姓名, 最多10个字符
        int s1,s2,s3;        //三门课的考试成绩 
        int total;           //总成绩   
    } ;
    typedef struct Student Student;  //声明了一个结构类型Student类型
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    输入格式:

    输入在一行中给出非负整数n(n⩾1)n(n\geqslant1)n(n1)。随后nnn行,每行给出一个学生的信息,格式为学号 学号 姓名 成绩1 成绩2 成绩3,中间以空格分隔。
    要求:
    学号姓名中不包括空白字符(空格、tab符)和空字符串。

    输出格式:

    在一行中输出总分最高学生的姓名、学号和总分,间隔一个空格。题目保证这样的学生是唯一的。

    输入样例:

    5
    2109001 HuangJie 78 83 79
    2109002 Liuhaipeng 79 80 77
    2109003 Wangqiang 87 86 76
    2109004 Liangfeng 92 89 79
    2109005 Chengmeng 80 82 75
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出样例:

    在这里给出相应的输出。例如:

    2109004 Liangfeng 260
    
    • 1
    编译器
    GXX
    代码
    #include 
    using namespace std;
    

    struct Student
    {
    string num; //学号,最多10个字符
    string name; //姓名, 最多10个字符
    int s1,s2,s3; //三门课的考试成绩
    int total; //总成绩
    bool operator < (const Student &s) const
    {
    return total < s.total;
    }
    } ;

    int main()
    {
    int n;
    cin>>n;

    priority_queue<Student> Q;
    for(int i=0; i<n; i++)
    {
        Student st;
        cin>>st.num>>st.name>>st.s1>>st.s2>>st.s3;
        st.total = st.s1+st.s2+st.s3;
        Q.push(st);
    }
    
    Student st;
    int flag;
    st = Q.top();
    Q.pop();
    flag = st.total;
    cout<<st.num<<' '<<st.name<<' '<<st.total<<'\n';
    
    while (!Q.empty())
    {
        st = Q.top();
        Q.pop();
        if (st.total == flag)
            cout<<st.num<<' '<<st.name<<' '<<st.total<<endl;
        else break;
    }
    
    
    return 0;
    
    • 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

    }

    评测结果
    答案正确 (5 分)
    测试点得分
    测试点结果得分耗时内存
    0答案正确33.00 ms324 KB
    1答案正确13.00 ms324 KB
    2答案正确13.00 ms320 KB

    7-5
    平面向量加法
    (10分)

    本题要求编写程序,计算两个二维平面向量的和向量。

    输入格式:

    输入在一行中按照“x1x_1x1 y1y_1y1 x2x_2x2 y2y_2y2”的格式给出两个二维平面向量v1=(x1,y1)v_1=(x_1, y_1)v1=(x1,y1)v2=(x2,y2)v_2=(x_2, y_2)v2=(x2,y2)的分量。

    输出格式:

    在一行中按照(x, y)的格式输出和向量,坐标输出小数点后一位(注意不能输出−0.0-0.00.0)。

    输入样例:

    3.5 -2.7 -13.9 8.7
    
    • 1

    输出样例:

    (-10.4, 6.0)
    
    • 1
    编译器
    GXX
    代码
    #include
    using namespace std;
    

    struct V
    {
    double x,y;
    };

    int main()
    {
    V v1,v2,v;
    cin>>v1.x>>v1.y;
    cin>>v2.x>>v2.y;
    v.x = v1.x + v2.x;
    v.y = v1.y + v2.y;

    if(fabs(v.y)<0.05) v.y=0;
    if(fabs(v.x)<0.05) v.x=0;
    printf("(%.1lf, %.1lf)\n",v.x,v.y);
    
    return 0;    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    }

    评测结果
    答案正确 (10 分)
    测试点得分
    测试点结果得分耗时内存
    0答案正确53.00 ms452 KB
    1答案正确53.00 ms452 KB

    7-6
    有理数比较
    (10分)

    本题要求编写程序,比较两个有理数的大小。

    输入格式:

    输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。

    输出格式:

    在一行中按照“a1/b1 关系符 a2/b2”的格式输出两个有理数的关系。其中“>>>”表示“大于”,“<<<”表示“小于”,“===”表示“等于”。

    输入样例1:

    1/2 3/4
    
    • 1

    输出样例1:

    1/2 < 3/4
    
    • 1

    输入样例2:

    6/8 3/4
    
    • 1

    输出样例2:

    6/8 = 3/4
    
    • 1
    编译器
    GXX
    代码
    #include 
    using namespace std;
    struct fraction
    {
        int sign;
        int fz;
        int fm;
    };
    

    fraction add(const fraction &a, const fraction &b)
    {
    fraction ans;
    if (a.sign == b.sign)
    {
    ans.sign = a.sign;
    ans.fz = a.fzb.fm+a.fmb.fz;
    ans.fm = a.fm * b.fm;
    }
    else
    {
    ans.fz = a.sign * a.fzb.fm + a.fmb.signb.fz;
    ans.fm = a.fm
    b.fm;
    if (ans.fz <0) ans.sign = -1;
    else ans.sign = 1;
    }
    int gcd = __gcd(ans.fz,ans.fm);
    ans.fz = ans.fz / gcd;
    ans.fm = ans.fm / gcd;

    return ans;
    
    • 1

    }

    fraction sub(const fraction &a, const fraction &b)
    {
    fraction c = b;
    c.sign = -c.sign;
    return add(a,c);
    }

    void print(const fraction &f)
    {
    if (f.fm == 1) cout<f.fz;
    else
    cout<f.fz<<'/'< }

    fraction read()
    {
    int z,m;
    char slash;
    fraction temp= {1,0,1};

    cin>>z>>slash>>m;
    temp.sign = (z>=0)?1:-1;
    temp.fz = temp.sign *z;
    temp.fm = m;
    
    return temp;    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    char fraction_compare(fraction &a,fraction &b)
    {
    fraction res = sub(a,b);

    if (res.sign ==1 && res.fz>0)
        return '>';
    else if(res.fz == 0)
        return '=';
    else return '<';    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    }

    int main()
    {
    fraction a=read();
    fraction b=read();
    fraction res = sub(a,b);
    print(a);
    cout<<’ ‘< print(b);
    return 0;
    }

    评测结果
    答案正确 (10 分)
    编译器输出
    a.cpp: In function ‘int main()’:
    a.cpp:76:14: warning: variable ‘res’ set but not used [-Wunused-but-set-variable]
    fraction res = sub(a,b);
    ^~~
    测试点得分
    测试点结果得分耗时内存
    0答案正确33.00 ms324 KB
    1答案正确33.00 ms324 KB
    2答案正确23.00 ms308 KB
    3答案正确23.00 ms448 KB

    7-7
    有理数加法
    (10分)

    本题要求编写程序,计算两个有理数的和。

    输入格式:

    输入在一行中按照a1/b1 a2/b2的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。

    输出格式:

    在一行中按照a/b的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。

    输入样例1:

    1/3 1/6
    
    • 1

    输出样例1:

    1/2
    
    • 1

    输入样例2:

    4/3 2/3
    
    • 1

    输出样例2:

    2
    
    • 1
    编译器
    GXX
    代码
    #include 
    using namespace std;
    struct fraction
    {
        int sign;
        int fz;
        int fm;    
    };
    

    fraction add(const fraction &a, const fraction &b)
    {
    fraction ans;
    if (a.sign == b.sign)
    {
    ans.sign = a.sign;
    ans.fz = a.fzb.fm+a.fmb.fz;
    ans.fm = a.fm * b.fm;
    }
    else
    {
    ans.fz = a.sign * a.fzb.fm + a.fmb.signb.fz;
    ans.fm = a.fm
    b.fm;
    (ans.fz <0)? (ans.sign = -1,ans.fz=-ans.fz):(ans.sign=1);

    }
    int gcd = __gcd(ans.fz,ans.fm);
    ans.fz = ans.fz / gcd;
    ans.fm = ans.fm / gcd;   
    
    return ans;    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    void print(const fraction &f)
    {
    if (f.fm == 1) cout<f.fz;
    else
    cout<f.fz<<'/'< }

    int main()
    {
    int n=2;
    //cin>>n;

    fraction res={1,0,1};
    int z,m;
    char slash;
    fraction temp={1,0,1};
    for(int i=1; i<=n; i++)
    {
        cin>>z>>slash>>m;
        temp.sign = (z>=0)?1:-1;
        temp.fz = temp.sign *z;
        temp.fm = m;
        
        res = add(res,temp);        
    }
    
    //res.fm = res.fm *n;
    
    int gcd = __gcd(res.fz,res.fm);
    res.fz = res.fz / gcd;
    res.fm = res.fm / gcd;   
    
    print(res);
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    }

    评测结果
    答案正确 (10 分)
    测试点得分
    测试点结果得分耗时内存
    0答案正确33.00 ms324 KB
    1答案正确32.00 ms328 KB
    2答案正确33.00 ms444 KB
    3答案正确12.00 ms320 KB

    7-8
    有理数均值
    (10分)

    本题要求编写程序,计算N个有理数的平均值。

    输入格式:

    输入第一行给出正整数N(≤\le100);第二行中按照a1/b1 a2/b2 …的格式给出N个分数形式的有理数,其中分子和分母全是整形范围内的整数;如果是负数,则负号一定出现在最前面。

    输出格式:

    在一行中按照a/b的格式输出N个有理数的平均值。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。

    输入样例1:

    4
    1/2 1/6 3/6 -5/10
    
    • 1
    • 2

    输出样例1:

    1/6
    
    • 1

    输入样例2:

    2
    4/3 2/3
    
    • 1
    • 2

    输出样例2:

    1
    
    • 1
    编译器
    GXX
    代码
    #include 
    using namespace std;
    struct fraction
    {
        int sign;
        int fz;
        int fm;    
    };
    

    fraction add(const fraction &a, const fraction &b)
    {
    fraction ans;
    if (a.sign == b.sign)
    {
    ans.sign = a.sign;
    ans.fz = a.fzb.fm+a.fmb.fz;
    ans.fm = a.fm * b.fm;
    }
    else
    {
    ans.fz = a.sign * a.fzb.fm + a.fmb.signb.fz;
    ans.fm = a.fm
    b.fm;
    ans.fz < 0 ? ans.sign = -1, ans.fz=-ans.fz: ans.sign=1;

    }
    int gcd = __gcd(ans.fz,ans.fm);
    ans.fz = ans.fz / gcd;
    ans.fm = ans.fm / gcd;   
    
    return ans;    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    void print(const fraction &f)
    {
    if (f.fm == 1) cout<f.fz;
    else
    cout<f.fz<<'/'< }

    int main()
    {
    int n;
    cin>>n;

    fraction res={1,0,1};
    int z,m;
    char slash;
    fraction temp={1,0,1};
    for(int i=1; i<=n; i++)
    {
        cin>>z>>slash>>m;
        temp.sign = (z>=0)?1:-1;
        temp.fz = temp.sign *z;
        temp.fm = m;
        
        res = add(res,temp);        
    }
    
    res.fm = res.fm *n;
    
    int gcd = __gcd(res.fz,res.fm);
    res.fz = res.fz / gcd;
    res.fm = res.fm / gcd;   
    
    print(res);
    
    return 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    }

    评测结果
    答案正确 (10 分)
    测试点得分
    测试点结果得分耗时内存
    0答案正确43.00 ms448 KB
    1答案正确23.00 ms448 KB
    2答案正确23.00 ms444 KB
    3答案正确23.00 ms436 KB

    7-9
    排列枚举
    (10分)

    口袋里有红、蓝、黄、黑4种颜色的球若干,每次从口袋先后取出3个球,问得到3种不同颜色的球的可能取法,输出每种排列的情况。球只能是4种颜色之一,而且判断各球是否同色,可以用枚举类型变量处理。

    输入格式:

    输出格式:

    输出所有排列。

    输入样例:

    在这里给出一组输入。例如:

    • 1

    输出样例:

    在这里给出相应的输出。例如:

    1 red blue yellow
    2 red blue black
    3 red yellow blue
    4 red yellow black
    5 red black blue
    6 red black yellow
    7 blue red yellow
    8 blue red black
    9 blue yellow red
    10 blue yellow black
    11 blue black red
    12 blue black yellow
    13 yellow red blue
    14 yellow red black
    15 yellow blue red
    16 yellow blue black
    17 yellow black red
    18 yellow black blue
    19 black red blue
    20 black red yellow
    21 black blue red
    22 black blue yellow
    23 black yellow red
    24 black yellow blue
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    编译器
    GXX
    代码
    #include 
    

    using namespace std;

    enum color{red,blue,yellow,black};
    string color_arr[4]={"red","blue","yellow","black"};

    int main()
    {
    int cnt = 0;
    for(int i=red;i<=black;i++)
    {
    for(int j = red;j<=black;j++)
    {
    for(int k = red;k<=black;k++)
    {
    if(i!=j&&j!=k&&i!=k)
    {
    cnt++;
    cout< }
    }
    }
    }

    return 0;
    
    • 1

    }

    评测结果
    答案正确 (10 分)
    测试点得分
    测试点结果得分耗时内存
    1答案正确103.00 ms440 KB

  • 相关阅读:
    条款41:针对可复制的形参,在移动成本低并且一定会被复制的前提下,考虑将其按值传递
    pytest--fixture的使用(前置、后置)
    [附源码]Python计算机毕业设计Django基于Java酒店管理系统
    怎样更改linux的用户名
    SpringBoot SpringBoot 原理篇 1 自动配置 1.13 bean 依赖属性配置
    java-net-php-python-ssm巴音学院本科部校园网站计算机毕业设计程序
    网页加载流程&&各种路径之间的区别
    每天一道算法题——动态规划
    Maven之aop框架
    动态主机配置协议(DHCP)解密:网络自动化与管理的关键
  • 原文地址:https://blog.csdn.net/qq_60755115/article/details/126542643