7-8 哈利·波特的考试(c语言)

该程序使用C语言编写,主要包含一个主函数和一个递归函数`bxmz`。程序读取输入数据,通过动态规划方法处理数组元素,计算特定条件下的最优解。它涉及到数组操作、条件判断以及递归调用,旨在解决一种特定的计算问题。
摘要由CSDN通过智能技术生成

在这里插入图片描述

#include<stdio.h>
int a,b;
int bxmz(int *d,int *e,int f,int g){
    int n=0,m,h;
    for(;n<a;n++){
        if(e[n]){
            if(f>n){
                h=d[f+a*n-(n+1)*(n+2)/2];
            }else if(f<n){
                h=d[n+a*f-(f+1)*(f+2)/2];
            }else continue;
            if(h){
                if(b>n){
                    m=b+a*n-(n+1)*(n+2)/2;
                }else if(b<n){
                    m=n+a*b-(b+1)*(b+2)/2;
                }else continue;
                if(g+h<d[m]||d[m]==0){
                    d[m]=g+h;
                    e[n]=0;
                    bxmz(d,e,n,g+h);
                    e[n]=1;
                }
            }
        }
    }
}
int main(){
    scanf("%d%d",&a,&b);
    int c=a*(a-1)/2,d[c],e[a],f[a+1],n=0,m;
    for(;n<c;n++){
        d[n]=0;
    }
    for(n=0;n<b;n++){
        scanf("%d%d%d",&f[0],&f[1],&f[2]);
        if(f[0]>f[1]){
            m=f[0];
            f[0]=f[1];
            f[1]=m;
        }
        d[f[1]-1-a+a*f[0]-f[0]*(f[0]+1)/2]=f[2];
    }
    for(n=1,e[0]=0;n<a;n++){
        e[n]=1;
    }
    for(c=n=0,f[0]=1,f[1]=0;f[n];n++){
        for(m=0;m<a;m++){
            if(e[m]){
                if(f[n]>m){
                    b=d[f[n]+a*m-(m+1)*(m+2)/2];
                }else if(f[n]<m){
                    b=d[m+a*f[n]-(f[n]+1)*(f[n]+2)/2];
                }else continue;
                if(b){
                    e[m]=0;
                    c++;
                    f[c]=m;
                    f[c+1]=0;
                }
            }
        }
    }
    if(n<a){
        printf("0");
        return 0;
    }
    for(n=1,e[0]=0;n<a;n++){
        e[n]=1;
    }
    for(n=f[0]=0,m=2;n<a;n++){
        for(;m<a;m++){
            if(n<m){
                c=m+a*n-(n+1)*(n+2)/2;
            }else if(m<n){
                c=n+a*m-(m+1)*(m+2)/2;
            }else continue;
            if(d[f[0]]<d[c]){
                f[0]=c;
            }
        }
        for(m=0,e[n]=0,b=n;m<a;m++){
            if(m==f[0])continue;
            if(n<m){
                c=d[m+a*n-(n+1)*(n+2)/2];
            }else if(m<n){
                c=d[n+a*m-(m+1)*(m+2)/2];
            }else continue;
            if(c){
                e[m]=0;
                bxmz(d,e,m,c);
                e[m]=1;
            }
        }
        f[0]=n-1;
        m=1;
        e[n]=1;
    }
    for(n=b=0;n<a;n++){
        for(m=0,e[n]=0;m<a;m++){
            if(m>n){
                c=d[m+a*n-(n+1)*(n+2)/2];
            }else if(m<n){
                c=d[n+a*m-(m+1)*(m+2)/2];
            }else continue;
            if(c>e[n]){
                e[n]=c;
            }
        }
        if(e[b]>e[n]){
            b=n;
        }
    }
    printf("%d %d",b+1,e[b]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值