排列<一>

理论和习题来源于书本,有些能用计算机模拟的题尽量用代码来解。

1.5个球放在3个不同的盒子里面,允许有盒子不放球,求有多少种可能?
解:穷举,设盒子A,B,C,每个盒子都有0~5个球的可能,但是三个盒子相加必定为5,得到代码:

for(int i=0;i<=5;i++)
        for(int j=0;j<=5;j++)
            for(int k=0;k<=5;k++)
                if(i+j+k==5)
                    cout<<i<<j<<k<<endl;

2.从n个不同元素中,任取m个(n>=m)不同元素,把这个m个元素有顺序地安排在一个单球上,我们叫从n个不同元素取出m个元素的无重复环排列,且环排列数为:
3.书看了一大半,突然想到本源问题,计算机如何模拟排列呢?比如说,排列1,2,3这三个数字,并打印它的所有可能。纸上计算是容易的,6种可能,可是计算机如何执行呢?
设想三个有序的盒子,A,B,C,一字排开,1,2,3做为元素可以放在三个盒子里面,显然是不能重复的。假如盒A放了1,那么盒B只能放2或3,假如盒B放了2,那盒C只能放3,关键的是,三个盒子都有三种可能!现在用for循环的原始办法:

    for(int i=1;i<=3;i++)/*盒子A*/
        for(int j=1;j<=3;j++)/*盒子B*/
        {
            if(i==j)/*不能相同于盒子A*/
                continue;
            else
            {
                for(int k=1;k<=3;k++)/*盒子C*/
                {
                    if(k==i || k==j)/*不能相同于盒子A或B*/
                    continue;
                    else /*此处可继续嵌套*/
                    cout<<i<<j<<k<<endl;
                }
            }
        }

如果有四个元素,可以在内部再嵌套。整个代码有规律的像锥子一般向右边延伸...这里采用的思路是逐个安置,先安排A,再安排B,再安排C....以此类推...虽然深度搜索也能解决,但显然,这个思路更简单和容易理解!
4.排列的改进:

/*三个数的排列,改进*/
#include <iostream>
using namespace std;
int a[3];
int b[3]={3,4,5};
int main()
{
    for(int i=0;i<3;i++)/*盒子A*/
    {
        a[0]=b[i];
        for(int j=0;j<3;j++)/*盒子B*/
        {
            if(a[0]==b[j])/*不能相同于盒子A*/
                continue;
            else
            {
                a[1]=b[j];
                for(int k=0;k<3;k++)/*盒子C*/
                {
                    if(a[0]==b[k] || a[1]==b[k])/*不能相同于盒子A或B*/
                    continue;
                    else /*此处可继续嵌套*/
                    {
                        a[2]=b[k];
                        for(int g=0;g<3;g++)/*得到一组排列*/
                        cout<<a[g]<<" ";
                        cout<<endl;
                    }
                }
            }
        }
    }
    return 0;
}
View Code

5.

  

转载于:https://www.cnblogs.com/tinaluo/p/5320618.html

<!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>EDA-Robot</title> <style> body { margin: 0; padding: 0; font-family: Arial, sans-serif; } .container { max-width: 800px; margin: 0 auto; padding: 20px; text-align: center; } h1 { text-align: center; } .button { display: inline-block; height: 30px; width: 300px; margin-top: 20px; padding: 10px 20px; background-color: deepskyblue; color: #fff; border: none; border-radius: 20px; /* 添加圆角 */ text-decoration: none; line-height: 2; /* 通过调整line-height的值来调整文字的垂直位置 */ text-align: center; /* 文字居中 */ box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2); /* 添加立体感 */ transition: all 0.3s ease; /* 添加过渡效果 */ } .button:hover { background-color: skyblue; /* 鼠标悬停时的背景颜色 */ transform: translateY(2px); /* 点击效果 */ box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3); /* 添加更多立体感 */ } .search-box { margin-top: 20px; display: inline-block; height: 30px; width: 300px; padding: 5px 10px; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 20px; text-align: center; /* 文字居中 */ } .hidden { display: none; /* 初始隐藏 */ } </style> </head> <body> <form action='/connect' method='POST'> <div class='container'> <h1>EDA-Robot设备配置页</h1> <p>本项目基于ESP8266主控开发</p> <input type='text' name='ssid' placeholder='输入WIFI212名称' class='search-box'> <input type='password' name='pass' placeholder='输入WIFI密码' class='search-box'> <input type='uid' name='uid' placeholder='输入bilibili用户ID' class='search-box'> <input type='api' name='api' placeholder='输入心知天气API密钥' class='search-box'> <input type='city' name='city' placeholder='输入城市拼音小写' class='search-box'> <input type='submit' style="height: 50px;width: 320px" class='button' value="保存"> <a href="https://lceda.cn/"> <table class="container button" style="height: 200px"> <tr> <th>设备名称:</th> <td>EDA-Robot</td> </tr> <tr> <th>内存大小:</th> <td>4MB</td> </tr> <tr> <th>控制台版本:</th> <td>V1.0</td> </tr> <tr> <th>作者:</th> <td> 臣除君</td> </tr> </table> </a> </div> </form> </body> </html>为什么这里的 <tr> <th>设备名称:</th> <td>EDA-Robot</td> </tr>是横向排版的?
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值