一些代码

n皇后
``

#include<bits/stdc++.h>
int cnt=0;
using namespace std;
int a[100];
int cno(int t)
{

    for(int i=0;i<t;i++)
    {
        if(a[i]==a[t])
            return 0;

        if(t-i==abs(a[t]-a[i]))
            return 0;


    }
    return 1;
}
void dfs(int t)
{
    if(t==8)
    {
        for(int i=0;i<8;i++)
            cout<<a[i]<<' ';
        cnt++;
        putchar('\n');
        return;
    }



    for(int i=0;i<8;i++)
    {
        a[t]=i;
        if(cno(t))
        dfs(t+1);
    }



}
int main()
{

    dfs(0);
    cout<<cnt;
    return 0;
}



矩阵连乘求最小计算量


#include<bits/stdc++.h>

using namespace std;
int a[10][10];
int p[7]={30,35,15,5,10,20,25};
int s[10][10];
void cnpo(int *p,int n,int m)
{
    for(int i=1;i<=m;i++)
        a[i][i]=0;

    for(int r=2;r<=m;r++)
    {

        for(int i=1;i<=m-r+1;i++)
        {
            int j=r+i-1;

            a[i][j]=a[i+1][j]+p[i-1]*p[i]*p[j];
            s[i][j]=i;
            for(int k=i+1;k<j;k++)
            {
                int temp=a[i][k]+a[k+1][j]+p[i-1]*p[k]*p[j];
                if(temp<a[i][j])
                {
                    a[i][j]=temp;

                    s[i][j]=k;
                }
            }

        }
    }

      cout<<a[1][m]<<endl;

}
void nuca(int n,int m)
{
    if(n==m)
        return;
   nuca(n,s[n][m]);

   nuca(s[n][m]+1,m);

   cout<<n<<','<<s[n][m]<<"and"<<s[n][m]+1<<','<<m<<endl;

}
int main()
{
    cnpo(p,1,6);

    nuca(1,6);

    return 0;
}


全排列问题

#include<bits/stdc++.h>
int cnt=0;
using namespace std;
int a[100];

void dop(int a[],int begin,int end)
{
    if(begin==end)
    {
        for(int i=0;i<5;i++)
            cout<<a[i]<<' ';

        cout<<endl;
        return;
    }
    for(int i=begin;i<=end;i++) ///以每一个数字作为起点
    {
        int temp=a[begin];  
        a[begin]=a[i];
        a[i]=temp;

        dop(a,begin+1,end);

       temp=a[begin];//换回去
        a[begin]=a[i];
        a[i]=temp;
    }
}
int main()
{

   int a[]={1,2,3,4,5};
   dop(a,0,4);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛郎恋刘娘,刘娘念牛郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值