zoukankan      html  css  js  c++  java
  • usaco 1.2.3 Name That Number

    View Code
     1 /*
    2 ID: qyxiang1
    3 PROG: namenum
    4 LANG: C++
    5 */
    6 #include <fstream>
    7 #include<string>
    8 #include<iostream>
    9 #include<memory.h>
    10 #include<algorithm>
    11
    12 using namespace std;
    13
    14 ifstream fin("namenum.in");
    15 ofstream fout("namenum.out");
    16
    17 #ifdef _DEBUG
    18 #define out cout
    19 #define in cin
    20 #else
    21 #define out fout
    22 #define in fin
    23 #endif
    24
    25 int num[] =
    26 {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,-1,7,7,8,8,8,9,9,9};
    27
    28 int main()
    29 {
    30 string str;
    31 in >> str;
    32
    33 int len ;
    34 len = str.length();
    35
    36 fstream dict_in("dict.txt");
    37
    38 string tmp;
    39 bool find = false;
    40 bool exist = false;
    41
    42 while(dict_in >> tmp)//会把字典都读取且仅读取一遍
    43 {
    44 if(tmp.length() == len)//只有长度相等时才进入进一步判断
    45 {
    46 find = true;
    47 for(int j = 0; j < len; ++j)
    48 {
    49 if(num[tmp[j]- 'A'] != str[j] - '0')//有任何一个字符不符合则退出循环
    50 {
    51 find = false;
    52 break;
    53 }
    54 }
    55
    56 if(find)
    57 {
    58 out << tmp << endl;
    59 exist = true;//一旦有输出即证明存在字符串
    60 }
    61 }
    62 }
    63
    64 if(!exist)
    65 out << "NONE" << endl;
    66
    67 return 0;
    68 }

    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 dictionarywhich 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
    



     

  • 相关阅读:
    装饰器模块和面试题
    装饰器和推导式
    设计商城系统,主要提供两个功能:商品管理、会员管理。
    写代码:三级菜单
    写代码:循环打印names列表,把元素和索引值都打印出来。
    写代码: 编写登录接口
    写代码:假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?
    写代码:输入一年份,判断该年份是否是闰年并输出结果。
    写代码:制作趣味模板程序
    变量n1和n2是什么关系
  • 原文地址:https://www.cnblogs.com/cosmoseeker/p/2139912.html
Copyright © 2011-2022 走看看