实验4

编程题

7.1

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
#include <stdio.h>
int main()
{
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
int a, b;
scanf("%d %d", &a, &b);
int SA = 0, SB = 0;
int temp_a = a;
int temp_b = b;
while (temp_a != 0) {
SA += temp_a % 10;
temp_a /= 10;
}

while (temp_b != 0) {
SB += temp_b % 10;
temp_b /= 10;
}
if (a % SB == 0&&b % SA != 0) {
printf("A\n");
} else if (b % SA == 0&&a % SB != 0) {
printf("B\n");
} else if (a > b) {
printf("A\n");
} else {
printf("B\n");
}
}

return 0;
}

7.2

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
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <math.h>
int fun1(int a);
int fun2(int a);
int fun3(int a);

int main()
{
int N,a=0,b=0;
scanf("%d",&N);
if(fun1(N)==1)
{
a=fun2(N);
printf("%d",a);
}
else{
b=fun3(N);
printf("%d",b);
}
}
int fun1(int n) {
if (n <= 1) {
return 0;
}
if (n == 2) {
return 1;
}
if (n % 2 == 0) {
return 0;
}
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int fun2(int a)
{
int sum=0;
while(a!=0)
{
sum+=a%10;
a=a/10;
}
return sum;

}

int fun3(int a)
{
if (a == 0)
{
return 0;
}
int sum=1;
while(a!=0)
{
sum*=a%10;
a=a/10;
}
return sum;

}

7.3

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
#include<stdio.h>
#include <limits.h>

int commondivisor(int a,int b){
int temp;
a=a>0?a:-a;
b=b>0?b:-b;
while(b!=0){
temp=b;
b=a%b;
a=temp;
}
return a;
}

int commonmultiple(int x,int y){
int a=x*y/commondivisor(x,y);
if(x<0&&y<0){
return -a;
}else{
return a;
}
}

int main()
{
int m,n;
while(scanf("%d %d",&m,&n)!=EOF){
if(m==0||n==0)
{
return 0;
}
if (n > 0 && m > 0)
{
if (n > INT_MAX / m)
{
continue;
}
}
printf("%d %d\n",commondivisor(m,n),commonmultiple(m,n));
}
return 0;
}

7.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int ackermann(int m, int n) {
if (m == 0) {
return n + 1;
} else if (n == 0) {
return ackermann(m - 1, 1);
} else {
return ackermann(m - 1, ackermann(m, n - 1));
}
}

int main() {
int m, n;
scanf("%d %d", &m, &n);
int result = ackermann(m, n);
printf("%d\n", result);
return 0;
}

7.5

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
#include <stdio.h>
void Title();
void Lyric();
void Win();
int main() {
Win();
return 0;}
void Title() {
printf("============\n");
printf(" 爱拼才会赢\n");
printf("============\n");
printf("词曲:陈百潭\n");
printf("演唱:叶启田\n");
}
void Lyric() {
printf("一时失志不免怨叹\n");
printf("一时落魄不免胆寒\n");
printf("那通失去希望\n");
printf("每日醉茫茫\n");
printf("无魂有体亲像稻草人\n");
printf("人生可比是海上的波浪\n");
printf("有时起有时落\n");
printf("好运歹运\n");
printf("总嘛要照起工来行\n");
printf("三分天注定\n");
printf("七分靠打拼\n");
printf("爱拼才会赢\n");
}
void Win() {
Title();
printf("\n");
Lyric();
printf("\n");
Lyric();
printf("\n");
Lyric();
}

7.6

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
#include <stdio.h>
float max(float d1, float d2, float d3);
int main() {
int n;
scanf("%d", &n);
float best_overall_score = 0.0;
int best_player_id = 0;
for (int i = 1; i <= n; i++) {
float s1, s2, s3;
scanf("%f %f %f", &s1, &s2, &s3);
float current_best = max(s1, s2, s3);
if (current_best > best_overall_score) {
best_overall_score = current_best;
best_player_id = i;
}
}

printf("%d %.2f\n", best_player_id, best_overall_score);

return 0;
}

float max(float d1, float d2, float d3) {
float temp_max = d1;

if (d2 > temp_max) {
temp_max = d2;
}
if (d3 > temp_max) {
temp_max = d3;
}

return temp_max;
}

作业4

6.1

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


int MaxDayMonth(int year, int month)
{
if (year <= 0 || month < 1 || month > 12) {
return 0;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if (IsLeapYear(year)) {
return 29;
} else {
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 0;
}
}

6.2

1
2
3
4
5
6
7
double result;
if (x != 5) {
result = 2 / fabs(5 - x);
} else {
result = exp(x);
}
return result;

6.3

1
2
3
4
5
6
7
8
9
10
int getDigit(long long n)
{
int count = 0;
do {
count++;
n /= 10;
} while (n > 0);

return count;
}

6.4

1
2
3
4
5
6
7
int fun(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fun(n - 1) + fun(n - 2);
}
}