zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    Redux React-redux 理解
    React ---- 浅谈ReactJs
    javascript --- 鼠标事件
    javaScript --- localStorage 本地存储
    javascript -- 对象(深拷贝、浅拷贝)
    javascript --- 数组输出数组中最大值最小值差值
    Css中未知 div 宽高度,水平垂直居中方法
    Windows下使用NCL(Cygwin模拟Linux环境)
    Qt加载网页(加载浏览器插件)和制作托盘后台运行(南信大财务报账看号)
    OpenGL超级宝典第5版&&GLSL法线变换
  • 原文地址:https://www.cnblogs.com/dplearning/p/3707652.html
Copyright © 2011-2022 走看看