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

C++Write a program that will prepare a shipping label and determine the cost for a box of any size and mass. five attributes: type double length, width, height, mass, price six attributes: type string senderName, receiverName, senderAddress, receiverAddress, originCountry, destinationCountry (you can shorten names if you like, just make a comment about their meaning) a default constructor – basic initial values for all parameters a user constructor – input sender Name, senderAddress, originCountry a double function volume() – a function to return l×w×h a double function surfArea() – a function to return 2×(l×w+l×h+w×h) a double function girth() – a function to return the perimeter for the two smallest dimensions P=2(x+y), where x and y are the two smalles of l,w,h a double function maxDim() – a function to return the largest dimension from l,w,h a double function pricing() – a function to return the shipping price based on the table above an operator+ function – a function that determines the total cost of shipping (return type double).... (you may decide how to calculate.... a) add prices from both packages, b) apply fee or discount for number of packages) an operator>> function – a function to get the information from the user about the package.... OR.... a set of “ask” functions to ask the same information an operator<< function – a function to type the information in the shipping label format.... OR.... a set of “print” functions – to print the same information
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值