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;
    }

  • 相关阅读:
    在dubbo工程中,使用druid监控
    docker在windows下上传文件到容器
    Docker容器Tomcat部署war包
    You are using the runtime-only build of Vue where the template compiler is not available. Either pre
    Vue项目用了脚手架vue-cli3.0,会报错You are using the runtime-only build of Vue where the template compiler is not available
    输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
    Github上传代码菜鸟超详细教程
    23种设计模式
    java面试
    latex输入希腊字母
  • 原文地址:https://www.cnblogs.com/amourjun/p/5134213.html
Copyright © 2011-2022 走看看