matlab 12-10

习题练习

判断成绩区间:小于60为E,70-60为D,70-80为C,80-90为B,90以上为A,用switch方法
解析:换算成小数(除以10就可以换算成小数),向下取整。

方法一

%小于60为E,60-79为D,70-79为C,80-89为B,90-100为A
clc
i=input('输入分数:');
i=i/10;
i=floor(i);
switch i
    case 10
        fprintf('A\n')
    case 9
        fprintf('A\n')
    case 8
        fprintf('B\n')
    case 7
        fprintf('C\n')
    case 6
        fprintf('D\n')
    otherwise

            fprintf('E\n')
    end

方法二

clc
clear
score = input('请输入成绩:');
s = floor(score/10);%通过将成绩/10再做一个向下取整,从而实现对成绩这个变量的离散化
switch s
    case 10
        fprintf('A\n')
    case 9
        fprintf('A\n')
    case 8
        fprintf('B\n')
    case 7
        fprintf('C\n')
    case 6
        fprintf('D\n')
    otherwise
        fprintf('E\n')
end

循环结构–for

这个2是步长值,表示循环间隔值
后面的10表示循环次数
在这里插入图片描述

在这里插入图片描述

案例一:用for循环语句写1+100

%计算1+2+3+...+100=?
clc
clear
s = 0;
for i = 1:100 %for语句会对循环变量进行赋初值的操作,同时指定循环变量的终值和步长值
    s = s+i;
end %end语句对循环变量做一个增加,用循环变量+步长值
fprintf('1+2+3+...+100=%d\n',s)

在这里插入图片描述

案例二:for+if写1+100

%计算1+2+3+...+100=?
clc
clear
s = 0;
for i = 1:100 %for语句会对循环变量进行赋初值的操作,同时指定循环变量的终值和步长值
    s = s+i;
end %end语句对循环变量做一个增加,用循环变量+步长值
fprintf('1+2+3+...+100=%d\n',s)

案例三:用while实现循环语句

while表示在不知道循环次数情况下可使用

%求1+2+3+...+101=?用while实现
clc
clear
i = 1;%i用来进行循环
s = 0;%s用来存储求和的结果
while i <= 101
    s = s+i;
    i = i+1;%用来对i进行递增,如果没有这条语句,i的结果始终为1
end
fprintf('1+2+3+...+101=%d\n',s)

结果:
在这里插入图片描述
方案二:不知道循环次数下使用while

%求1+2+3+...+m=n,n是小于等于100且最接近100的整数
clc
clear
i = 0;
s = 0;
while s+i <= 100
    s = s+i;
    i = i+1;
end
fprintf('1+2+3+...+%d=%d\n',i-1,s)

在这里插入图片描述

习题–生成99乘法表

%生成九九乘法表
clc
clear
for i = 1:9
    for j = 1:i
        fprintf('%d * %d = %d ',i,j,i*j)
    end
    fprintf('\n')
end

习题二

有100元,公鸡3元1只,母鸡5元1只,小鸡1元3只,100元正好买100只鸡,问公鸡、母鸡、小鸡各多少只?
cock公鸡 hen母鸡 chicken小鸡

%有100元,公鸡3元1只,母鸡5元1只,小鸡1元3只,100元正好买100只鸡,问公鸡、母鸡、小鸡各多少只

    clc
    clear
    for cock = 1:100
        for hen = 1:100
            for chicken=1:100
                if cock + hen + chicken == 100 & cock * 3 + hen * 5 + chicken / 3 == 100
                    fprintf('公鸡%d只,母鸡%d只,小鸡%d只\n',cock,hen,chicken)
                end
            end
        end
    end
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Language of Technical Computing MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java™. You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing. Key Features • High-level language for numerical computation, visualization, and application development • Interactive environment for iterative exploration, design, and problem solving • Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, numerical integration, and solving ordinary differential equations • Built-in graphics for visualizing data and tools for creating custom plots • Development tools for improving code quality and maintainability and maximizing performance • Tools for building applications with custom graphical interfaces • Functions for integrating MATLAB based algorithms with external applications and languages such as C, Java, .NET, and Microsoft® Excel®

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值