Matlab的if语句switch语句for循环while循环语句练习
2015-04-25 09:56
691 查看
1、 输入一组整数a,输出其中奇偶数、奇偶数的和、积以及个数。
m文件代码
其他方法(下面的方法输入必须是矩阵形式,如:[2 3 4 5 6 7 8],前一个用除法,后一个用find函数)
2、计算s=e-(1/1+1/1!+1/2!+...+1/N!)使得s<10^(-6),求最小的N
m文件代码
结果为9
注:阶乘除了可以用factorial(i),还可以用prod(1:i),prod是连乘函数
3、 试计算以下循环语句将进行多少步操作
(1) for i= —32768:32767
(2) for j= 32768:32767
(3) for k=2:4:3
(4) for m=ones(5,5)
m文件代码
结果为65536、0、1、5
4、 观察以下循环语句,试计算每个循环次数和循环结束之后ires或x的值
(1) ires=1;
while mod(ires,10)~=0
ires=ires+1;
end
(2) ires=2;
while ires<=200
ires=ires^2;
end
(3) ires=2;
while ires>200
ires=ires^2;
end
(4)x = 500;
while x > 0
if isprime(x)
break;
end
x = x -1;
end
m文件代码
结果为:10、256、2、449
注:isprime(x)为求素数函数,如果是素数返回1,否则返回0
ISPRIME True for prime numbers.
ISPRIME(X) is 1 for the elements of X that are prime, 0 otherwise.
5、分别用if和switch多分支语句计算税款,用户输入货价,输出相应的税款:
货价<2000,免税;
货价在2000到5000之间,超过2000部分抽税2%;
5000以上,除2%以外,5000以上抽税5%,加收手续费60元。
if语句
m文件代码
switch语句
m文件代码
6、分别用if和while做,m=1+2+2^2+...+2^n,直到1000为止,求最大的N
只用if的话,用递归可以实现
保存下面两个m文件
sum2n.m
然后调用sum2nbigm(1000,0),结果为9
while循环方法
m文件代码
7、‘The quick brown fox jumps over the lazy dog’,26个字母至少出现过一次,统计每个字母出现的次数。
逐个遍历即可
方法一:sum函数遍历
m文件代码
方法二:逐个遍历累加
m文件代码
8、输入一个字符,如果是大写字母,输出小写;如果是小写字母,输出大写;其他字符,原样输出。
m文件
9、利用rand函数编制一个新的函数rand10,该函数能够产生在[-10,10]之内的随机数。
rand10.m
10、对上题的函数加以修改,使得产生的随机数在[low,high]之间,其中low和high为用户输入参数。
randLowToHigh.m
m文件代码
clear clc n=input('输入数字个数'); for i=1:n x(i)=input('输入数字:'); end j=1;k=1; %y向量存奇数,z向量存偶数 for i=1:n if mod(x(i),2) y(j)=x(i); j=j+1; else z(k)=x(i); k=k+1; end end %输出奇数和偶数 y z %输出所有奇数和、所有偶数和 sum(y) sum(z) %输出所有奇数连乘积、所有偶数乘积 prod(y) prod(z) %输出所有奇数个数、所有偶数个数 length(y) length(z)
其他方法(下面的方法输入必须是矩阵形式,如:[2 3 4 5 6 7 8],前一个用除法,后一个用find函数)
clear a=input('input some numbers:') n=length(a); j=0;k=0; for i=1:n if rem(a(i),2)==0 j=j+1; b(j)=a(i); else k=k+1; c(k)=a(i); end end a b,j c,k ---------------------- a=input('input some numbers:') b=a(find(rem(a,2)==0)) j=length(b) c=a(find(rem(a,2)~=0)) k=length(c) -------------
2、计算s=e-(1/1+1/1!+1/2!+...+1/N!)使得s<10^(-6),求最小的N
m文件代码
clear clc s=exp(1); i=0; while(1) s=s-1.0/factorial(i); if(s<1e-6) break; end i=i+1; end i
结果为9
注:阶乘除了可以用factorial(i),还可以用prod(1:i),prod是连乘函数
3、 试计算以下循环语句将进行多少步操作
(1) for i= —32768:32767
(2) for j= 32768:32767
(3) for k=2:4:3
(4) for m=ones(5,5)
m文件代码
count=0; for i= -32768:32767 count=count+1; end count count=0; for j= 32768:32767 count=count+1; end count count=0; for k=2:4:3 count=count+1; end count count=0; for m=ones(5,5) count=count+1; end count
结果为65536、0、1、5
4、 观察以下循环语句,试计算每个循环次数和循环结束之后ires或x的值
(1) ires=1;
while mod(ires,10)~=0
ires=ires+1;
end
(2) ires=2;
while ires<=200
ires=ires^2;
end
(3) ires=2;
while ires>200
ires=ires^2;
end
(4)x = 500;
while x > 0
if isprime(x)
break;
end
x = x -1;
end
m文件代码
ires=1; while mod(ires,10)~=0 ires=ires+1; end ires ires=2; while ires<=200 ires=ires^2; end ires ires=2; while ires>200 ires=ires^2; end ires x = 500; while x > 0 if isprime(x) break; end x = x -1; end x
结果为:10、256、2、449
注:isprime(x)为求素数函数,如果是素数返回1,否则返回0
ISPRIME True for prime numbers.
ISPRIME(X) is 1 for the elements of X that are prime, 0 otherwise.
5、分别用if和switch多分支语句计算税款,用户输入货价,输出相应的税款:
货价<2000,免税;
货价在2000到5000之间,超过2000部分抽税2%;
5000以上,除2%以外,5000以上抽税5%,加收手续费60元。
if语句
m文件代码
clear clc price=input('请输入价格:'); taxes=0; fee=0; if price<2000 taxes=0; elseif price<5000 taxes=(price-2000)*0.02; else taxes=(5000-2000)*0.02+(price-5000)*0.05; fee=60; end taxes fee
switch语句
m文件代码
clear clc price=input('请输入价格:'); taxes=0; fee=0; switch floor(price/1000) case {0,1} taxes=0; case {2,3,4} taxes=(price-2000)*0.02; otherwise taxes=(5000-2000)*0.02+(price-5000)*0.05; fee=60; end taxes fee
6、分别用if和while做,m=1+2+2^2+...+2^n,直到1000为止,求最大的N
只用if的话,用递归可以实现
保存下面两个m文件
sum2n.m
%matlab递归计算1+2+2^2+...+2^n function num=sum2n(n) if n==0 num=1; else num=2^n+sum2n(n-1); endsum2nbigm.m
%判断1+2+2^2+...+2^n是否大于m,参数为m,n的初值(n的初值建议取0) %对于本题调用方法为sum2nbigm(1000,0) function num=sum2nbigm(m,n) if sum2n(n)>m num=n; else num=sum2nbigm(m,n+1); end
然后调用sum2nbigm(1000,0),结果为9
while循环方法
m文件代码
clear clc m=0; i=0; while m<1000 m=m+2^i; i=i+1; end n=i-1; n
7、‘The quick brown fox jumps over the lazy dog’,26个字母至少出现过一次,统计每个字母出现的次数。
逐个遍历即可
方法一:sum函数遍历
m文件代码
%利用sum函数遍历 clear clc str='The quick brown fox jumps over the lazy dog'; str=lower(str); %将字符串中的大写转换为小写 for i=1:26 x(i)=sum(str==char('a'-1+i)); end x
方法二:逐个遍历累加
m文件代码
%逐个遍历累加 clear clc str='The quick brown fox jumps over the lazy dog'; str=lower(str); z=zeros(1,26); for i=1:length(str) if(str(i)<='z'&&str(i)>='a') z(str(i)-'a'+1)=z(str(i)-'a'+1)+1; end end z
8、输入一个字符,如果是大写字母,输出小写;如果是小写字母,输出大写;其他字符,原样输出。
m文件
clear clc n=input('请输入一个字符','s'); n=n(1);%即使输入多个字符也只取第一个 if(n>='a'&&n<='z') n=char(n-32); elseif(n>='A'&&n<='Z') n=char(n+32); else n=n; end n
9、利用rand函数编制一个新的函数rand10,该函数能够产生在[-10,10]之内的随机数。
rand10.m
function num=rand10() num=rand()*20-10; end
10、对上题的函数加以修改,使得产生的随机数在[low,high]之间,其中low和high为用户输入参数。
randLowToHigh.m
%调用示例:randLowToHigh(-50,100) function num=randLowToHigh(low,high) num=rand()*(high-low)+low; end
相关文章推荐
- 分支语句 循环语句 if,switch,do,while,for分析
- if,switch,for循环语句
- 分支语句(if,switch case)、循环(for,while,do while)、continue、break、随机数
- Go语言学习笔记 --- 控制语句之if判断,循环语句for, switch语句和跳转语句
- matlab if&&for&&switch语句
- 选择 | 循环 | 语句(if,if……else,switch,while,for)的理解和使用
- 循环控制语句for while do-while if switch
- (二) php if语句,switch语句,continue语句,return语句,for 、while、do while 循环
- +++++++脚本练习之for循环和if语句使用
- if条件和for循环语句、while、do..while、switch语法
- Lesson_for_java_day05--java的三目运算符、if语句、switch语句、while循环、for循环
- JavaScript-for循环语句,if语句,switch语句
- 分支结构、循环结构流控语句(if、switch、for、while)
- python_3(循环语句、if、switch、for、break、冒泡算法)
- 【练习】题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句
- c++中的结构化语句 判断语句if 分支语句switch 循环语句 while 和 do while 循环语句for的使用
- JAVA之旅(二)——if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结
- 《iOS移动开发从入门到精通》图书连载14: if条件判断语句、switch条件判断语句、 continue语句和forthrough语句
- 求1+2+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A? B:C)
- 【九度1506】求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。