NJUCSE17

2017拔尖班编程能力测试

2017-09-30 by Fancy

代码太丑,请勿吐槽

问题 A: Sum of Numbers

输入十个实数,输出正数的和,负数的和,总和,保留两位小数

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
double x, ans1, ans2;
int main() {
for(int i = 1; i <= 10; i++){
scanf("%lf", &x);
if(x>0) ans1 += x;
else ans2 += x;
}
printf("%.2lf,%.2lf,%.2lf", ans1, ans2, ans1 + ans2);
}

问题 B: Mad Lib

输入a.老师的名字,b.你的名字,c.食物,d.数字,e.形容词,f.颜色,g.动物,输出请假条
Dear Instructor [Instructor name],
I am sorry that I am unable to turn in my homework at this time. First, I ate a rotten [food], which made me turn [color] and extremely ill. I came down with a fever of [number]. Next, my [adjective] pet [animal] must have smelled the remains of the [food] on my homework, because he ate it. I am currently rewriting my homework and hope you will accept it late.
Sincerely,
[Your name]

1
2
3
4
5
6
7
#include <cstdio>
char s[10][100];
int main() {
for(int i = 1; i <= 7; i++) scanf("%s", s[i]);
printf("Dear Instructor %s,\nI am sorry that I am unable to turn in my homework at this time. First, I ate a rotten %s, which made me turn %s and extremely ill. I came down with a fever of %s. Next, my %s pet %s must have smelled the remains of the %s on my homework, because he ate it. I am currently rewriting my homework and hope you will accept it late.\nSincerely,\n%s",
s[1], s[3], s[6], s[4], s[5], s[7], s[3], s[2]);
}

太长了可读性下降,于是变成了下面的,将就着看吧

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
char s[10][100];
int main() {
for(int i = 1; i <= 7; i++) scanf("%s", s[i]);
printf("Dear Instructor %s,\nI am sorry that I am unable to turn in my homework at this time. ", s[1]);
printf("First, I ate a rotten %s, which made me turn %s and extremely ill. ", s[3], s[6]);
printf("I came down with a fever of %s. Next, my %s pet %s must have ", s[4], s[5], s[7]);
printf("smelled the remains of the %s on my homework, because he ate it. ",s[3]);
printf("I am currently rewriting my homework and hope you will accept it late.\nSincerely,\n%s",s[2]);
}

问题 C: Paper-rock-scissor

石头(P)剪刀(R)布(S),输出结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
int w[5][5], x, y;
char a[5], b[5];
int main() {
w[1][3] = w[3][4] = w[4][1] = 1;
w[3][1] = w[4][3] = w[1][4] = 2;
while(scanf("%s%s", a, b) != EOF) {
if(a[0] >= 'a') x = a[0] - 'o';
else x = a[0] - 'O';
if(b[0] >= 'a') y = b[0] - 'o';
else y = b[0] - 'O';
if(x == y) printf("Nobody wins.\n");
else {
printf("User %d wins", w[x][y]);
if(x + y == 4) printf("(Paper covers rock).\n");
else if(x + y == 7) printf("(Rock breaks scissors).\n");
else printf("(Scissors cut paper).\n");
}
}
}

问题 D: 平方和

给定 $a1,a2,a3,a4,b1,b2,b3,b4$,输出 $c1,c2,c3,c4$,使得 $c1^2+c2^2+c3^2+c4^2=(a1^2+a2^2+a3^2+a4^2)(b1^2+b2^2+b3^2+b4^2)$

四平方和定理

$(a^2 + b^2 + c^2 + d^2)(x^2 + y^2 + z^2 + w^2) = $
$(ax + by + cz + dw)^2 + (ay - bx + cw - dz)^2 + (az - bw - cx + dy)^2 + (aw + bz - cy - dx)^2$

1
2
3
4
5
6
7
8
9
#include <cstdio>
#include <cmath>
int n, a, b, c, d, x, y, z, w;
int main() {
for(scanf("%d",&n); n; n--) {
scanf("%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &x, &y, &z, &w);
printf("%d %d %d %d\n", a*x+b*y+c*z+d*w, a*y-b*x+c*w-d*z, a*z-b*w-c*x+d*y, a*w+b*z-c*y-d*x);
}
}

问题 E: 组合数计算

计算 $C(n,r) = \frac{n!}{r!(n-r)!}$。请首先使用递归实现方法 long long factorial(int n) 计算阶乘 n!;然后借助 factorial 方法实现问题所求(我们将检查程序代码)。

1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>
int n, m;
long long factorial(int x)
{
if(!x) return 1;
return x * factorial(x-1);
}
int main() {
scanf("%d%d", &n, &m);
printf("%lld\n", factorial(n) / factorial(m) / factorial(n-m));
}

问题 F: Constructing Roads

有n个点,两点间距离给定,有一些点对之间已经连有边。求最段的建边长度,是n个点相互连通。

最小生成树:Prim 算法和 Kruskal 算法

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
#include <algorithm>
#include <cstdio>
const int N = 105;
int n, Q, tota, ans, g[N][N], fa[N];
struct node {
int x, y, v;
}a[N*N<<1];
inline bool cmp (const node &a, const node &b) {
return a.v < b.v;
}
int father(int x) {
return fa[x]==x ? x : fa[x]=father(fa[x]);
}
inline bool Union(int x, int y) {
int f1 = father(x), f2 = father(y);
if(f1 != f2) fa[f1] = f2;
return f1 != f2;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) fa[i] = i;

for(int i = 1, x; i <= n; i++)
for(int j = 1; j <= n; j++) {
scanf("%d", &x);
if(i < j) a[++tota].x = i, a[tota].y = j, a[tota].v = x;
}
std::sort(a+1, a+tota+1, cmp);

scanf("%d", &Q);
for(int i = 1, x, y; i <= Q; i++) {
scanf("%d%d", &x, &y);
Union(x, y);
}
for(int i = 1; i <= tota; i++)
if(Union(a[i].x, a[i].y)) ans += a[i].v;
printf("%d\n", ans);
}
Tags: 编程