A题
编程将输入的一个n位整数按由小到大的顺序输出。如输入78945,则输出45789。
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
45clc,clear;
a = input('please input a number:');
aa = num2str(a);
b = length(aa);
c = zeros(1,b);
result = 0;
for i=1:b
d = mod(a,10);
c(i) = d;
a = (a-mod(a,10))/10;
end
c = sort(c);
for j=1:b
result = c(j) + result*10;
end
fprintf('The answer is :%d\n',result)
%法二
clc,clear;
a = input('please input a number:');
c=[];result = 0;b=0;
while a>0
d = mod(a,10);
c = [c,d];
a = (a-mod(a,10))/10;
b=b+1;
end
c = sort(c);
for j=1:b
result = c(j) + result*10;
end
fprintf('The answer is :%d\n',result)
%法三
clc,clear;
a = input('a = ');
b = num2str(a);
c = length(b);
d = [];
for i = 1:c
d(i) = str2num(b(i));
end
sort(d)在同一坐标系中作出以下式子表示的图形x^2+y^2=1,y=x^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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80法一
clc,clear;
close all;
syms x
f1 = 'x^2+y^2-1';
f2 = 'y-x^2';
ezplot(f1, [-1 1] , [-1 1]);
hold on
ezplot(f2, [-1 1] , [-1 1]);
%法二
clc,clear;
close all;
x=-1:0.01:1;
for i=1:length(x)
y1(i)=sqrt(1-x(i)^2);
y2(i)=-sqrt(1-x(i)^2);
y3(i)=x(i)^2;
end
plot(x,y1,'b',x,y2,'b',x,y3,'b')
%法三
clc,clear;
close all;
x=-1:0.01:1;
y1=sqrt(1-x.^2);
y2=-sqrt(1-x.^2);
y3=x.^2;
plot(x,y1,'b',x,y2,'b',x,y3,'b')
```
# C题
- 第4题
```matlab
% 4、三对新人参加婚礼,其中新郎为A、B、C,新娘为X、Y、Z,某人不知道他们谁和谁结婚,于是分别问了其中的三位,得到如下回答:
% A:我将和X结婚;
% X:我将和C结婚;
% C:我将和Z结婚。
% 已知他们三人都在说谎,请编程找出谁将和谁结婚。(20分)
%X-B C-Y A-Z
%设A B C于其结婚对象分别设为1 2 3。
clc,clear;
A=1;B=2;C=3;
for i=1:3
for j=1:3
for z=1:3
if i~=A&&i~=C&&z~=C&&i~=j&&i~=z&&j~=z
X = i;
Y = j;
Z = z;
break
end
end
end
end
D=[X Y Z];d=['X' 'Y' 'Z'];
m=find(D==A);
n=find(D==B);
v=find(D==C);
fprintf('与A结婚的是%s\n',d(m))
fprintf('与B结婚的是%s\n',d(n))
fprintf('与C结婚的是%s\n',d(v))第五题
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% a = [17,8,11,22,3,27,4;
% 41,3,19,5,13,11,7;
% 8,14,21,18,14,16,23;
% 31,22,4,25,9,15,17;
% 13,24,21,6,17,22,31;
% 11,2,34,15,22,4,13;
% 16,3,7,19,13,27,21;];
% (1)求这个矩阵四周元素的均值,并将它们删除。(10分)
% (2)重复(1)的操作直到只剩下一个元素。(20分)
%第二问
%%
%先保存函数再调用主函数
function b=myfunction(a)
[m,n]=size(a);
b=a(2:m-1,2:n-1);
end
%%
%主函数
clc,clear;
a = [17,8,11,22,3,27,4;
41,3,19,5,13,11,7;
8,14,21,18,14,16,23;
31,22,4,25,9,15,17;
13,24,21,6,17,22,31;
11,2,34,15,22,4,13;
16,3,7,19,13,27,21;];
c=length(a);
while c~=1
a=myfunction(a);
c=length(a);
end
fprintf('the answer is %d\n',a)
%法二
clc,clear;
a = [17,8,11,22,3,27,4;
41,3,19,5,13,11,7;
8,14,21,18,14,16,23;
31,22,4,25,9,15,17;
13,24,21,6,17,22,31;
11,2,34,15,22,4,13;
16,3,7,19,13,27,21;];
c=length(a);
while c~=1
[m,n]=size(a);
a=a(2:m-1,2:n-1);
c=length(a);
end
fprintf('the answer is %d\n',a)