zoukankan      html  css  js  c++  java
  • USA Name That Number

    Name That Number

    Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the members of their herd by a pleasing name rather than saying, "C'mon, #4734, get along."

    Help the poor cowhands out by writing a program that will translate the brand serial number of a cow into possible names uniquely associated with that serial number. Since the cow hands all have cellular saddle phones these days, use the standard Touch-Tone(R) telephone keypad mapping to get from numbers to letters (except for "Q" and "Z"):

              2: A,B,C     5: J,K,L    8: T,U,V            3: D,E,F     6: M,N,O    9: W,X,Y            4: G,H,I     7: P,R,S  

    Acceptable names for cattle are provided to you in a file named "dict.txt", which contains a list of fewer than 5,000 acceptable cattle names (all letters capitalized). Take a cow's brand number and report which of all the possible words to which that number maps are in the given dictionary which is supplied as dict.txt in the grading environment (and is sorted into ascending order).

    For instance, the brand number 4734 produces all the following names:

    GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI  GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI  GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI  HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI  HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI  IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI  ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI  

    As it happens, the only one of these 81 names that is in the list of valid names is "GREG".

    Write a program that is given the brand number of a cow and prints all the valid names that can be generated from that brand number or ``NONE'' if there are no valid names. Serial numbers can be as many as a dozen digits long. 
    PROGRAM NAME: namenum
    INPUT FORMAT
    A single line with a number from 1 through 12 digits in length. 
    SAMPLE INPUT (file namenum.in) 

    4734  


    OUTPUT FORMAT
    A list of valid names that can be generated from the input, one per line, in ascending alphabetical order. 
    SAMPLE OUTPUT (file namenum.out)

    GREG  

    ==================================================================================================

    此题对自己来说算是挺新的,期中包括了对文件内容的递归,声明ifstream file ("dict.txt"); 然后用file>>来输入文件内容从而对其中的内容递归.

    此题思路,由于编号长度太长  12个编号的3次方 若根据所输入的编号进行组合然后再文件中查找,数据量会很大,而若将其字母与数字先匹配,先判断编号长度,再从文件中递归查找,递归文件中的组合判断是否有与编号相匹配的即可.

    /*
    ID: jun41821
    PROG: namenum
    LANG: C++
    */
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    using namespace std;
    ofstream fout ("namenum.out");
    ifstream fin ("namenum.in");
    ifstream file ("dict.txt");

    bool noans;
    string s, name;
    char map[256];
    int i, sl, nl;

    int main(){
        noans = true;
        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';

        fin>>s; sl = s.length();
        while(file>>name){              //在文件中读取一个name
            nl = name.length();         //记录长度
            if (sl != nl) continue;     //若长度不一致,读取下一个name
            for (i = 0; i < sl; i++) if (s[i] != map[name[i]]) break;   //否则.判断是否与所输入的代号匹配
            if (i == sl){                                   //若完全匹配
                fout<<name<<endl;               //输出
                noans = false;
            }
        }
        if (noans) fout<<"NONE"<<endl;          //否则输出NONE
        //system("pause");
        return 0;
    }

  • 相关阅读:
    linux中RabbitMQ安装教程
    linux中的文件权限chmod
    ceph架构简介
    利用双重检查锁定和CAS算法:解决并发下数据库的一致性问题
    对接第三方服务引起的小思考-回调和Sign算法
    <<Java并发编程的艺术>>-阅读笔记和思维导图
    SpringBoot2+Netty打造通俗简版RPC通信框架(升级版)
    SpringBoot2+Netty打造通俗简版RPC通信框架
    [安全] Kali Linux (debian)系统使用记录
    [安全] nmap工具的使用
  • 原文地址:https://www.cnblogs.com/amourjun/p/5134213.html
Copyright © 2011-2022 走看看