ZOJ 2207 听说是简单题,结果我是被折磨得半死写出来的代码还特丑

本文介绍了一种使用枚举法解决特定字符串排列问题的方法,通过遍历所有可能的ABCDE字符组合来确定输入字符串的排名。该算法适用于解决ZOJ 2207题目中的排名计算任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单题对我这样的新手也很困难啊

 

//////////////////////////////////////////////////////////
//    ZOJ: 2207
//    简单题:通过枚举出所有ABCDE五个字母组成的120种组合然后
//            判断排名。
//////////////////////////////////////////////////////////
#include "stdio.h"

int ranking(char *duibi, char *input);    //用于每个数组里面对数的个数

int main(void)
{
    //枚举出所有情况的字母组合
    char sort[150][50] = {0};
    char szbuf[50] = {"AAAAA"};
    char temp[50] = {'A','B','C','D','E'};
    int i, j, x, y;
    int l = 0;
    int z;
    int k;

    for (i = 0; i < 5; i++)
    {
        szbuf[0] = temp[i];

        for (j = 0; j < 5; j++)
        {
            szbuf[1] = temp[j];

            if (szbuf[0] != szbuf[1])
            {
                for (x = 0; x < 5; x++)
                {   
                    szbuf[2] = temp[x];

                    if (szbuf[2] != szbuf[1] && szbuf[2] != szbuf[0])
                    {
                        for (y = 0; y < 5; y++)
                        {
                            szbuf[3] = temp[y];

                            if (szbuf[3] != szbuf[0] && szbuf[3] != szbuf[1] && szbuf[3] != szbuf[2])
                            {
                                for (z = 0; z < 5; z++)
                                {
                                    szbuf[4] = temp[z];

                                    if (szbuf[4] != szbuf[0] && szbuf[4] != szbuf[1]
                                        && szbuf[4] != szbuf[2] && szbuf[4] != szbuf[3])
                                    {
                                        for (k = 0; k < 6; k++)
                                        {
                                            sort[l][k] = szbuf[k];
                                        }

                                        l++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
   
    int n;

    while(scanf("%d", &n))
    {
        if (n == 0)
            break;
       
        int value[200] = {0};
        int count = 0;
        int m;
        char cal[10] = {0};
        int resValue = -1;
        int m1;
        int index;

        while (n-- > 0)
        {
            scanf("%s", cal);
            //循环做判断并把值用一个结果数组保存
            for (m = 0; m < 120; m++)
            {
                value[m] += ranking(sort[m], cal);
            }
        }
        //从120字符串序列的结果值中找出距离值最小的并返回下标
        for (m1 = 0; m1 < 120; m1++)
        {
            if (resValue == -1)
            {
                resValue = value[m1];
                index = m1;
            }
            else
            {
                if (resValue > value[m1])
                {
                    resValue = value[m1];
                    index = m1;
                }
            }

        }

        printf("%s is the median ranking with value %d./n", sort[index], resValue);

    }

    return 0;
}
//用于判断的函数计算出每个字符串对比的结果距离值
int ranking(char *duibi, char *input)
{
    int result = 0;
    int i;
    int dui[10], in[10];
    //分别找出A B C D E五个字母的位置
    for (i = 0; i < 5; i++)
    {
        if (duibi[i]  == 'A')
            dui[0] = i;
        else if (duibi[i]  == 'B')
            dui[1] = i;
        else if (duibi[i]  == 'C')
            dui[2] = i;
        else if (duibi[i]  == 'D')
            dui[3] = i;
        else if (duibi[i] == 'E')
            dui[4] = i;
    }
    //参照字符串
    for (i = 0; i < 5; i++)
    {
        if (input[i]  == 'A')
            in[0] = i;
        else if (input[i]  == 'B')
            in[1] = i;
        else if (input[i]  == 'C')
            in[2] = i;
        else if (input[i]  == 'D')
            in[3] = i;
        else if (input[i] == 'E')
            in[4] = i;
    }
   
    int x, y, z = 0;
    int x1, y1, z1 = 0;
    int resa[100], resb[100];
    int o;
    //将五个字母中所有可能值列出
    for (x = 0; x < 5; x++)
    {
        for (y = x + 1; y < 5; y++)
        {
            resa[z++] = dui[x] - dui[y];
        }
    }

    for (x1 = 0; x1 < 5; x1++)
    {
        for (y1 = x1 + 1; y1 < 5; y1++)
        {
            resb[z1++] = in[x1] - in[y1];
        }
    }
    //搜索有产生排名变化的值 发生变化则结果值+1
    for (o = 0; o < z; o++)
    {
        if ((resa[o] > 0 && resb[o] < 0) || (resa[o] < 0 && resb[o] > 0))
        {
            ++result;
        }
    }

    return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值