function xpbombs(arg1,arg2,arg3)
%XPBOMBS Play the minesweeper game.
% There are 13 bombs hidden in the mine field. Try to flag them
% and uncover all of the other spaces without getting blown up.
% In each non-bomb square is printed the number of adjacent
% squares which contain bombs.
%
% Use the FLAG button to toggle in and out of flag mode. When
% in flag mode, clicking on any covered square places a flag on it.
% Clicking on any flag removes it.
%
% At any time during the game, the number of remaining unflagged
% bombs (assuming every flag is on a bomb) is shown in the upper left.
%
% NEW stops the game and creates a new minefield.
%
% CLOSE closes the game window.
% Copyright 1984-2015 The MathWorks, Inc.
persistent h minefield cntmines rows cols t nummines remaining
persistent HEIGHT WIDTH BWIDTH BHEIGHT textHndl
persistent flagHndl bomb hidden flag hb nhb hflag
if nargin <1,
arg1 = 'start'
end
if strcmp(arg1,'start')
rows = 8 % 16
cols = 8 % 30
nummines = 13 % 99
BWIDTH = 35
SWIDTH = 0
BHEIGHT = 35
SHEIGHT = 0
WIDTH = BWIDTH + SWIDTH
HEIGHT = BHEIGHT + SHEIGHT
程序接口
新版本的MATLAB可以利用MATLAB编译器和C/C++数学库和图形库,将自己的MATLAB程序自动转换为独立于MATLAB运行的C和帆察C++代码。允许用户编写可以和MATLAB进行交互的C或C++语言程序。
另外,MATLAB网页服务程序还容许在Web应用中使用自己的MATLAB数学和图形程序。MATLAB的一个重要特色就是具有一套程序扩展系统和一组称之为工具箱的特殊态滑茄应用子程序。工具箱是MATLAB函数的子程序库,每一个工具箱都是为某一类学科专业和应用而定制的,主要包括信号处理、控制系统、神经网络、模糊逻辑、小波分析和系统让升仿真等方面的应用。
以下是一份MATLAB程序,使用筛法求含裤得小于10^8的雷打数:```matlab
n = 1e8% 设置筛法范围
isPrime = true(n, 1)% 初始化数组为全部为true
isPrime(1) = false% 定义1不是素数
% 首先从2开始,将所有的素数的倍数标记为非素数
for i = 2:sqrt(n)
if isPrime(i)
isPrime(i * i : i : n) = false
end
end
% 输出小于10^8的雷打数
for i = 1:length(isPrime)
if mod(i, 4) == 1 &&isPrime(i)
fprintf('%d\n', i)
end
end
```
在程序中,首先定义了一个筛法范围 `n = 1e8`,然后利用逻辑数组 `isPrime` 来记录每个数是否为素数。初始化时,所有的数默认为素数,将数漏者组全部初始化为 `true`;同谈搜简时,定义数字1不是素数。之后,从2开始循环,将所有的素数的倍数标记为非素数。具体实现的方法是,如果检测到一个素数i,则将i * i及其之后的所有i的倍数都标记为非素数。
最后,再次循环判断并输出所有小于10^8的雷打数。因为所有的雷打数的形式为4k+1,所以对于满足此条件的素数,都可以视为雷打数。程序使用 `mod(i, 4) == 1` 检查当前数是否满足4k+1的形式,并使用 `isPrime(i)` 检查该数是否为素数。如果两个条件都满足,则输出该数即可。
需要注意的是,在程序中使用了 `sqrt(n)` 函数,以降低时间复杂度。因为n以内的素数最大可能为 `sqrt(n)`,所以只需要进行到 `sqrt(n)` 的筛法,即可得到全部小于n的素数。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)