⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twentyfour.m

📁 在matlab环境下
💻 M
字号:
%一个小游戏!!
function twentyfour
clear;str1='d';
mys='请用如下四个数字凑出24!';
s1='请输入你的算式:';
s3='你失败了,请继续努力 !!';
s2='你成功了!!!!!!!!';
s4='结束游戏请输入stop!';
s5='查阅正确答案,请安A健!.其中a-*b表示b-a,a\b表示b/a!!!!!';
while strcmp(str1,'stop')==0
    clc;
    ts=10*sum(clock);a=rem(ceil(ts),10)+1;b=rem(king(ceil(ts),a),10)+1;c=rem(a+b,10)+1;d=rem(a+b+c,10)+1;
    disp(mys);
disp(a) ;disp(',') ;disp(b); disp(',') ;disp(c);disp(','); disp(d); disp('.');

str=input(s1,'s');
val=str2num(str);
if val==24
    disp(s2);
else  disp(s3);
end
str2=input(s5,'s');
if strcmp(str2,'a')
    rand24(a,b,c,d);
end
str1=input(s4,'s');
end
!开始rand24函数......
function rand24(a,b,c,d)
fin=0;
for m=1:3
    [mat1 mat2]=macreat(a,b,c,d,m);
    for i=1:6
        for k=1:6
            for j=1:6
                if mymath([mymath(mat1,i),mymath(mat2,k)],j)<=24-0.001&mymath([mymath(mat1,i),mymath(mat2,k)],j)>=24+0.001
                    fin=1;break;end 
            end
            if fin==1 break;end 
        end
        if fin==1 break;end
    end
    if fin==1 break;end 
end
function y=mymath(x,bk)
    y=999999;
if bk==1
    y=x(1)+x(2);
else if bk==2
        y=x(1)-x(2);
    else if bk==3
            y=x(2)-x(1);
        else if bk==4
                y=x(1)*x(2);
            else if bk==5&x(2)~=0
                    y=x(1)/x(2);
                else if bk==6&x(1)~=0
                        y=x(2)/x(1);
                    end
                end
            end
        end
    end
end
end
    function [mt1 mt2]=macreat(a,b,c,d,n)
        if n==1
            mt1=[a,b];mt2=[c,d];
        else if n==2
                mt1=[a,c];mt2=[b,d];
            else if n==3
                    mt1=[a,d];mt2=[b,c];
                end
            end
        end
    end
    function [out1 out2]=select43(x,n)
        out1=x(n);out2=x([1:n-1,n+1:4]);end
    function [out1 out2]=select32(x,n)
        out1=x(n);out2=x([1:n-1,n+1:3]);end
if fin==0
    mymat=[a,b,c,d];
    for n=1:4
        [arg1 arg2]=select43(mymat,n);
        for l=1:3
             
         [arg3 g]=select32(arg2,l); 
         for z=1:6
             for h=1:6
                 for s=1:6
                     if mymath([mymath([mymath(g,s),arg3],h),arg1],z)<=24+0.0001&mymath([mymath([mymath(g,s),arg3],h),arg1],z)>=24-0.0001
                         fin=1;break;end
                 end
                 if fin==1 break;end
             end
             if fin==1 break;end 
         end
         if fin==1 break;end 
        end
     if fin==1 break;end 
    end
 if fin==1
    mystr=['(','(',num2str(g(1)),mysign(s),num2str(g(2)),')',mysign(h),num2str(arg3),')',mysign(z),num2str(arg1)];
        disp(mystr);end 
else if fin==1  
   mystr=['(',num2str(mat1(1)),mysign(i),num2str(mat1(2)),')',mysign(j),'(',num2str(mat2(1)),mysign(k),num2str(mat2(2)),')'];
     disp(mystr);end     
 end   
 if fin==0 disp('can not solve the problem');end  
  
end
!结束rand24函数
    function y=mysign(n)
        y='e';
        switch n
            case 1
                y='+';
            case 2
                y='-';
            case 3
                y='-*';
            case 4
                y='*';
            case 5
                y='/';
            case 6
                y='\';
        end
    end
end
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -