<!doctype html>
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 <stdio.h>
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;
}
5-2
读入一个整型数表示的秒数,将它转换为时分秒格式并输出。
#include <stdio.h>
struct TIME {
int sec, min, hour;
} time;
int main() { int t;
scanf("%d", &t);
time.hour = <span data-reactroot=""><input type="text" style="max-width:none;width:19.2ch;max-height:500px" value="t / 3600" disabled=""/><span style="margin-left:4px;color:gray">(2分)</span></span>;
time.min = <span data-reactroot=""><input type="text" style="max-width:none;width:32.4ch;max-height:500px" value="(t - time.hour * 3600) / 60" disabled=""/><span style="margin-left:4px;color:gray">(2分)</span></span>;
time.sec = t % 60;
printf ("<span data-reactroot=""><input type="text" style="max-width:none;width:19.2ch;max-height:500px" value="%02d:%02d:%02d" disabled=""/><span style="margin-left:4px;color:gray">(2分)</span></span>\n", time.hour, time.min, time.sec);
return 0;
}
输入样例
4508
输出样例
01:15:08
函数题
得分:15
总分:15
6-1
本题要求实现一个计算复数之积的简单函数。
函数接口定义:
struct complex multiply(struct complex x, struct complex y);
其中struct complex
是复数结构体,其定义如下:
struct complex{
int real;
int imag;
};
裁判测试程序样例:
#include <stdio.h>
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;
}
/* 你的代码将被嵌在这里 */
输入样例:
3 4 5 6
输出样例:
(3+4i) * (5+6i) = -9 + 38i
代码
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;
};
编译器输出
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
答案正确
5
2.00 ms
188 KB
1
答案正确
3
2.00 ms
372 KB
2
答案正确
2
2.00 ms
196 KB
6-2
已知空间中两点 A(x1,y1,z1)A(x_1,y_1, z_1) A ( x 1 , y 1 , z 1 ) 和 B(x2,y2,z2)B(x_2,y_2, z_2) B ( x 2 , y 2 , z 2 ) 之间的欧氏距离定义为:(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} ( x 1 − x 2 ) 2 + ( y 1 − y 2 ) 2 + ( z 1 − z 2 ) 2
。
本题要求实现一个函数,计算平面上两点之间的欧氏距离,平面上点的坐标通过以下结构给出:
struct point {
double x, y, z;
};
函数接口定义:
// 计算并返回平面上两点 a 和 b 之间的欧氏距离
double distance(struct point a, struct point b);
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
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;
}
void read_point(struct point *p) { scanf("%lf %lf %lf", &p->x, &p->y, &p->z); }
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
0 0 0 3 0 4
输出样例:
在这里给出相应的输出。例如:
5.000000
代码
// 计算并返回平面上两点 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;
}
编译器输出
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
答案正确
3
2.00 ms
188 KB
sample2
答案正确
2
2.00 ms
328 KB
编程题
得分:75
总分:75
7-1
复数是由两个实数分别作为实部和虚部构成的一个复合数,从另一个角度来说复数就是由两个实数构成的有序对,在C语言中适合用结构类型来表示复数。现在要求用结构类型
typedef struct
{
float x;
float y;
} Comp;
及其变量来表示与存储复数,编写程序实现复数的加减法运算。
输入格式:
在一行输入四个用空格分开的实数a1 b1 a2 b2
分别表示复数c1 = a1 + b1i
和c2 = 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
输出样例:
8.00+6.00i
2.00+2.00i
代码
#include<stdio.h>
#include<math.h>
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;
}
Comp multiply(Comp x, Comp y) { float real, imag; real = x.xy.x - x.y y.y; imag = x.yy.x + x.x y.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.y 0) 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); }
编译器输出
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
答案正确
2
2.00 ms
320 KB
2
答案正确
1
2.00 ms
188 KB
3
答案正确
1
2.00 ms
184 KB
4
答案正确
1
2.00 ms
188 KB
7-2
本题目要求先输入正整数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 张三 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
代码
#include <bits/stdc++.h>
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;
}
测试点得分
测试点
结果
得分
耗时
内存
0
答案正确
5
3.00 ms
448 KB
1
答案正确
3
3.00 ms
444 KB
2
答案正确
2
3.00 ms
448 KB
7-3
定义一个结构体类型表示一个圆(x,y,r),圆心坐标是(x,y),圆半径是r。从键盘输入一个圆的圆心坐标和半径,坐标值和半径均为整型数据,输出这个圆的面积,面积为float。面积公式为area=3.14∗r∗rarea =3.14r r a re a = 3.14 ∗ r ∗ r .
输入格式:
从键盘输入圆的圆心坐标和半径,之间用空格分隔
输出格式:
输出数据保留两位小数。
输入样例:
在这里给出一组输入。例如:
3 4 5
输出样例:
在这里给出相应的输出。例如:
78.50
代码
#include <bits/stdc++.h>
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
答案正确
5
3.00 ms
444 KB
7-4
给定n(n⩾1)n(n\geqslant1) n ( n ⩾ 1 ) 个学生的学号、姓名和3门课的考试成绩。编写程序找出总分最高的学生,并输出其学号、姓名和总分。如果有多个相同的最高分,则输出所有最高分学生的信息。要求: 存储学生信息及考试成绩的变量用如下结构类型来定义。
struct Student
{
char num[11]; //学号,最多10个字符
char name[11]; //姓名, 最多10个字符
int s1,s2,s3; //三门课的考试成绩
int total; //总成绩
} ;
typedef struct Student Student; //声明了一个结构类型Student类型
输入格式:
输入在一行中给出非负整数n(n⩾1)n(n\geqslant1) n ( n ⩾ 1 ) 。随后nn n 行,每行给出一个学生的信息,格式为学号 学号 姓名 成绩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
输出样例:
在这里给出相应的输出。例如:
2109004 Liangfeng 260
代码
#include <bits/stdc++.h>
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;
}
测试点得分
测试点
结果
得分
耗时
内存
0
答案正确
3
3.00 ms
324 KB
1
答案正确
1
3.00 ms
324 KB
2
答案正确
1
3.00 ms
320 KB
输入格式:
输入在一行中按照“x1x_1 x 1 y1y_1 y 1 x2x_2 x 2 y2y_2 y 2 ”的格式给出两个二维平面向量v1=(x1,y1)v_1=(x_1, y_1) v 1 = ( x 1 , y 1 ) 和v2=(x2,y2)v_2=(x_2, y_2) v 2 = ( x 2 , y 2 ) 的分量。
输出格式:
在一行中按照(x, y)
的格式输出和向量,坐标输出小数点后一位(注意不能输出−0.0-0.0 − 0.0 )。
输入样例:
3.5 -2.7 -13.9 8.7
输出样例:
(-10.4, 6.0)
代码
#include<bits/stdc++.h>
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;
}
测试点得分
测试点
结果
得分
耗时
内存
0
答案正确
5
3.00 ms
452 KB
1
答案正确
5
3.00 ms
452 KB
输入格式:
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。
输出格式:
在一行中按照“a1/b1 关系符 a2/b2”的格式输出两个有理数的关系。其中“>> > ”表示“大于”,“<< < ”表示“小于”,“== = ”表示“等于”。
输入样例1:
1/2 3/4
输出样例1:
1/2 < 3/4
输入样例2:
6/8 3/4
输出样例2:
6/8 = 3/4
代码
#include <bits/stdc++.h>
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.fm b.fz; ans.fm = a.fm * b.fm; } else { ans.fz = a.sign * a.fzb.fm + a.fm b.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;
}
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.signf.fz; else cout<<f.sign f.fz<<'/'<<f.fm; }
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;
}
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 '<';
}
int main() { fraction a=read(); fraction b=read(); fraction res = sub(a,b); print(a); cout<<’ ‘<<fraction_compare(a,b)<<’ '; print(b); return 0; }
编译器输出
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
答案正确
3
3.00 ms
324 KB
1
答案正确
3
3.00 ms
324 KB
2
答案正确
2
3.00 ms
308 KB
3
答案正确
2
3.00 ms
448 KB
输入格式:
输入在一行中按照a1/b1 a2/b2
的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。
输出格式:
在一行中按照a/b
的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
输入样例1:
1/3 1/6
输出样例1:
1/2
输入样例2:
4/3 2/3
输出样例2:
2
代码
#include <bits/stdc++.h>
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.fm b.fz; ans.fm = a.fm * b.fm; } else { ans.fz = a.sign * a.fzb.fm + a.fm b.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;
}
void print(const fraction &f) { if (f.fm == 1) cout<<f.signf.fz; else cout<<f.sign f.fz<<'/'<<f.fm; }
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;
}
测试点得分
测试点
结果
得分
耗时
内存
0
答案正确
3
3.00 ms
324 KB
1
答案正确
3
2.00 ms
328 KB
2
答案正确
3
3.00 ms
444 KB
3
答案正确
1
2.00 ms
320 KB
输入格式:
输入第一行给出正整数N(≤\le ≤ 100);第二行中按照a1/b1 a2/b2 …
的格式给出N个分数形式的有理数,其中分子和分母全是整形范围内的整数;如果是负数,则负号一定出现在最前面。
输出格式:
在一行中按照a/b
的格式输出N个有理数的平均值。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
输入样例1:
4
1/2 1/6 3/6 -5/10
输出样例1:
1/6
输入样例2:
2
4/3 2/3
输出样例2:
1
代码
#include <bits/stdc++.h>
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.fm b.fz; ans.fm = a.fm * b.fm; } else { ans.fz = a.sign * a.fzb.fm + a.fm b.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;
}
void print(const fraction &f) { if (f.fm == 1) cout<<f.signf.fz; else cout<<f.sign f.fz<<'/'<<f.fm; }
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;
}
测试点得分
测试点
结果
得分
耗时
内存
0
答案正确
4
3.00 ms
448 KB
1
答案正确
2
3.00 ms
448 KB
2
答案正确
2
3.00 ms
444 KB
3
答案正确
2
3.00 ms
436 KB
7-9
口袋里有红、蓝、黄、黑4种颜色的球若干,每次从口袋先后取出3个球,问得到3种不同颜色的球的可能取法,输出每种排列的情况。球只能是4种颜色之一,而且判断各球是否同色,可以用枚举类型变量处理。
输入格式:
无
输出格式:
输出所有排列。
输入样例:
在这里给出一组输入。例如:
无
输出样例:
在这里给出相应的输出。例如:
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
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<<cnt<<" "<<color_arr[i]<<" "<<color_arr[j]<<" "<<color_arr[k]<<endl; } } } }
return 0;
}
测试点得分
测试点
结果
得分
耗时
内存
1
答案正确
10
3.00 ms
440 KB