蓝桥杯算法提高 9-3摩尔斯电码

这里写图片描述
这里写图片描述
写这个题的时候出现了一些小问题,哎,还是因为太菜了呀。例如用二位数组存摩尔斯电码,刚开始用char code[26][5]这种形式,运行过程中出现很多问题,于是就百度了一下该如何存。万能的度娘给出的解释是:接受字符串首地址的数据类型应该为指针型,所以应该:char *code[26]。改了之后就运行成功了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char a[10000];
int main()
{
    char *code[26];
    code[0]="*-";
    code[1]="-***";
    code[2]="-*-*";
    code[3]="-**";
    code[4]="*";
    code[5]="*-*";
    code[6]="--*";
    code[7]="****";
    code[8]="**";
    code[9]="*---";
    code[10]="-*-";
    code[11]="*-**";
    code[12]="--";
    code[13]="-*";
    code[14]="---";
    code[15]="*--*";
    code[16]="--*-";
    code[17]="*-*";
    code[18]="***";
    code[19]="-";
    code[20]="**-";
    code[21]="***-";
    code[22]="*--";
    code[23]="-**-";
    code[24]="-*--";
    code[25]="--**";
    gets(a);
    //scanf("\n");
    int n,ans=0;
    char temp[1000];
    memset(temp,'\0',sizeof(temp));
    n=strlen(a);
    for(int i=0; i<n; i++)
    {
        if(a[i]=='*'||a[i]=='-')
        {
            temp[ans++]=a[i];
        }
        if(a[i]=='|'||i==n-1)
        {
            for(int j=0; j<26; j++)
            {
                int len1,len2;
                len1=strlen(temp);
                len2=strlen(code[j]);
                if(len1==len2)
                {
                    if(!strcmp(temp,code[j]))
                    {
                        cout<<(char)(j+97);
                        ans=0;
                        memset(temp,'\0',sizeof(temp));
                    }
                }
            }

        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值