• 【USACO】namenum


    //开始傻×了 受题目形容的误导 一心想生成所有可能的 字符串组合 之后查找非常慢 
    //听了同学的 将5000个dict里的字符串 转换成char型数组(不能直接用int 会越界)直接用输入的数据对着转换后的数据查找就可以了 
    
    //现在的程序还是太复杂了 没有必要提前把dict中的字符串都存起来 取一个判断一个就好了
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    typedef struct
    {
        char num[13];
        char name[13];
    }MYDICT;
    vector<MYDICT> dict; //vector<>中 最好不要用指针 因为vector对指针是不会复制的 如果之后用指针做了别的操作 vector里的值就都变了
    
    int transName2Num(char * name, char * num)
    {
        int len = strlen(name);
        int i;
        int n;
        for(i = 0, n = 0; i < len; i++)
        {
            switch(name[i])
            {
            case 'A':
            case 'B':
            case 'C': num[i] = '2';break;
            case 'D':
            case 'E':
            case 'F': num[i] = '3';break;
            case 'G':
            case 'H':
            case 'I': num[i] = '4';break;
            case 'J':
            case 'K':
            case 'L': num[i] = '5';break;
            case 'M':
            case 'N':
            case 'O': num[i] = '6';break;
            case 'P':
            case 'R':
            case 'S': num[i] = '7';break;
            case 'T':
            case 'U':
            case 'V': num[i] = '8';break;
            case 'W':
            case 'X':
            case 'Y': num[i] = '9';break;
            default: break;
            }
        }
        num[len] = '';
        return 0;
    }
    
    int main()
    {
        MYDICT tmp;
        int flag = 0;
        FILE * intxt, *out;
        char name[13];
        char num[13];
        char name3[13];
        fstream in;
        in.open("namenum.in", ios::in);
        intxt = fopen("dict.txt", "r");
        out = fopen("namenum.out", "w");
    
        char innum[13];
        in >> innum;
        do
        {
            fscanf(intxt,"%s", name);
            transName2Num(name, num);
            strcpy(tmp.name,name);
            strcpy(tmp.num,num);
            dict.push_back(tmp);
        }while(strcmp(name, "ZYTA") != 0);
        
        vector<MYDICT>::const_iterator it2;
        
        for(it2 = dict.begin(); it2 < dict.end(); it2++)
        {
            strcpy(name3,(*it2).num);
            if(strcmp(name3,innum) == 0)
            {
                fprintf(out, "%s
    ", (*it2).name);
                flag = 1;
            }
        }
    
        if(flag == 0)
        {
            fprintf(out, "NONE
    ");
        }
    
        
        return 0;
    }

    自己的代码还是太复杂了 下面是答案的 超精简

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
        FILE *in = fopen ("namenum.in", "r");
        FILE *in2 = fopen ("dict.txt", "r");
        FILE *out = fopen ("namenum.out","w");
        int nsolutions = 0;
        int numlen;
        char word[80], num[80], *p, *q, map[256];
        int i, j;
        map['A'] = map['B'] = map['C'] = '2';
        map['D'] = map['E'] = map['F'] = '3';
        map['G'] = map['H'] = map['I'] = '4';
        map['J'] = map['K'] = map['L'] = '5';
        map['M'] = map['N'] = map['O'] = '6';
        map['P'] = map['R'] = map['S'] = '7';
        map['T'] = map['U'] = map['V'] = '8';
        map['W'] = map['X'] = map['Y'] = '9';
        fscanf (in, "%s",num);
        numlen = strlen(num);
        while (fscanf (in2, "%s", word) != EOF) {
            for (p=word, q=num; *p && *q; p++, q++) {
                if (map[*p] != *q)
                    break;
            }
            if (*p == '' && *q == '') {
                fprintf (out, "%s
    ", word);
                nsolutions++;
            }
        }
        if (nsolutions == 0) fprintf(out,"NONE
    ");
        return 0;
    }
  • 相关阅读:
    [人物存档]AI【捏脸数据】【捏人数据】【人物卡精选】少女3DCG设计参考萌萌的耳朵
    [人物存档]AI【捏脸数据】【捏人数据】【人物卡精选】少女3DCG设计参考
    碧蓝航线-cosplay-吾妻猉 绘画CG设计参考
    Pixiv日榜2020-4-5精选动漫插画壁纸 设计参考
    Marvelous Designer 旗袍设计参考素材免费分享及服装3D模型导入MD教程
    [人物存档]AI【捏脸数据】【捏人数据】【人物卡精选】少女设计参考
    P站日榜【2020-3-22】pixiv动漫美图热门插画壁纸
    Pixiv腿部渔网袜绘画设计参考特辑,在我眼里你就是一个弟弟(姐弟特辑)绘画参考
    Daz3D studio 快速上手进阶免费教程,持续更新
    Airflow 1.10安装
  • 原文地址:https://www.cnblogs.com/dplearning/p/3707652.html
Copyright © 2020-2023  润新知