matlab deckShuffling

% a script to determine how many perfect shuffles are required
% before a given deck of cards returns to its original order

clear all, close all

% problem statement:
% suppose we have a deck of 8 (distinct) cards
% 1 2 3 4 5 6 7 8

% define a perfect shuffle as interleaving the cards as follows
% 1 5 2 6 3 7 4 8

% in other words, given a deck of an even number of distinct cards,
% divide the deck in half,
% then alternately insert cards from the bottom half into the top half

% for a deck of 8 cards, it is easily seen that the original order
% is obtained after 3 applications of this process

% the task is to write a program that tests this out for N cards,
% where we are particularly interested in the case N=52,
% the size of a standard deck of cards

% here are the basic steps:
% get the size of the deck
% check that the input is valid
% initialize deck to unique cards
% repeat:
%    perfectly shuffle the deck
%    keep track of how many shuffles have been made
% until: original order has been obtained

disp(‘Counting perfect shuffles of decks with an even number of cards.‘)
disp(‘ ‘)

N = input(‘Please enter size of deck: ‘);

% check that N is a (positive!) even number; else fail
if ((N < 0) | (rem(N,2) ~= 0)),
  error(‘Deck must contain an even number of cards.‘);
end

% initialize deck to unique cards
originalDeck = [1:N]‘;
shuffles     = 0;

% perform the first shuffle explicitly
shuffledDeck = perfectShuffle(originalDeck);
shuffles     = shuffles + 1;

while (~isequal(shuffledDeck,originalDeck)),
  shuffledDeck = perfectShuffle(shuffledDeck);
  shuffles     = shuffles + 1;
end

disp([‘A deck of size ‘, num2str(N), ‘ requires ‘, num2str(shuffles), ...
       ‘ perfect shuffles before it returns to its original state.‘])
时间: 2024-08-10 17:53:17

matlab deckShuffling的相关文章

利用MATLAB进行曲线拟合

软件环境:MATLAB2013a 一.多项式拟合 多项式拟合是利用多项式最佳地拟合观测数据,使得在观测数据点处的误差平方和最小. 在MATLAB中,利用函数ployfit和ployval进行多项式拟合. 函数ployfit根据观测数据及用户指定的多项式阶数得到光滑曲线的多项式表示,polyfit的一般调用格式为:P = polyfit(x,y,n).其中x为自变量,y为因变量,n为多项式阶数. polyval的输入可以是标量或矩阵,调用格式为 pv = polyval(p,a) pv = pol

MATLAB检查指定路径中的子文件夹中的文件名中是否带有空格

测试文件夹为: clear;close all;clc; %% %程序实现的功能 %检查指定路径中的子文件夹中的文件名中是否带有空格,并去掉文件名中的空格 %% %程序中用到的之前不清楚的函数如下 %1)strfind(a,b):即找a中是否有b,如果a中有b,则输出b的位置序号.没有输出空数组 %2)isempty(a):判断数组是否为空 %3)strrep(a,b,c):就是把a中所有出现的b换为c %4)movefile(a,b):a移动为b,如C:\test1.jpg移动为C\test2

关于MATLAB处理大数据坐标文件2017620

暑假已至,接下来组内成员将会各回各家,各找各妈,这肯定是对本次大数据比赛是很不利的. 接下来我会把任务分配给组员,当然任务会比起初的时候轻一点,因为我认为本次比赛的目的并不是我要求组员做什么,而是我的组员要求自己做什么! 我们现在主要接触的两门语言: MATLAB语言在数据处理方面很牛,它的画图功能也是杠杠的,尤其是3D画图 Python语言是一门近几年很火的语言,学好它对自己肯定只有益处,它的出生很晚,但是短短十多年,它已经稳居计算机语言前三名.尤其是现在的大数据时代,它的代码不仅简单易懂,而

linux用命令行运行matlab的.mat文件

入m文件所在目录后,运行 $ matlab -nodesktop -nosplash -r matlabfile 只用文件名matlabfile,不能添加.m

对AM信号FFT的matlab仿真

普通调幅波AM的频谱,大信号包络检波频谱分析 u(t)=Ucm(1+macos ?t)cos ?ct ma称为调幅系数 它的频谱由载波,上下边频组成 , 包络检波中二极管截去负半周再用电容低通滤波,可以得到基带信号,那么,截去负半周后的AM信号必定包含基带信号的频谱.我们可以通过matlab来验证. %已知基带信号为1hz,载波为64hz,调制系数ma=0.3,采样频率1024hz,FFT变换区间N为2048 clear; fs=1024; f=1; %1hz基带信号 fc=64; %64hz载

Matlab中使用jython扩展功能

Matlab中面向对象能力并不强,通过使用jython引擎能够对其功能扩展. 1 编辑classpath.txt增加jython.jar 在matlab中输入 which classpath.txt 结果: /usr/local/MATLAB/R2013a/toolbox/local/classpath.txt 编辑该文件,加入 /home/your_user/jython2.5.3/jython.jar 2 又一次启动matlab 3 编写代码測试 import javax.script.In

MATLAB(5)——生成归一化直方图

作者:桂. 时间:2017-03-10  22:13:36 链接:http://www.cnblogs.com/xingshansi/p/6533579.html 声明:欢迎转载,不过记得注明出处哦~ 前言 本文作为:曲线拟合与分布拟合 一文的补充内容,主要介绍MATLAB直方图的绘制,以及对应归一化直方图的实现.全文分三部分简单介绍: 1)直方图(hist)绘制: 2)栅栏图(bar)绘制: 3)归一化直方图实现. 一.直方图(hist) 可以对hist的直方图进行限定,两种途径:个数模式(n

Matlab:线性与非线性规划

Matlab的运筹与决策问题 线性规划问题 函数: linprog(f,A,B,Aep,Bep,lb,ub) 参数分析: f:目标函数的系数排列 A:约束条件的系数矩阵 B:约束条件的增广矩阵的结果 Aep:等式的系数矩阵 Bep:等式的结果矩阵 lb:所求解的最小值 ub:所求解的最大值 非线性规划问题(二次) 函数 quadprog(f,A,B,Aep,Bep,lb,ub) 非线性规划问题(普遍性) 函数 x=fmincon(’fun’,X0,A,b,Aeq,Beq,VLB,VUB,’non

matlab学习笔记第四章——统计和MATLAB编程介绍

1.柱状图: >> x = [55,63,69,70,75,78,82,84,85,88,90,96,100]; >> y = [1,2,1,6,4,7,2,1,3,2,4,2,1]; >> bar(x,y) 2.我们可以使用barh命令产生水平的柱状图. 3.通过调用mean函数,MATLAB会告诉我们一组数据的均值是多少. 4.