USACO Name That Number(File)

题目请点我

题解:
题目应该不难理解,就是找出数字对应字典中的字符串。因为数字对字符是一对多的关系,所以输入的数字可能对应多个答案。如果每次都要一一对比查找的话显然太浪费时间。转换思路,对于字典中的字符串,他们都有自己唯一对应的数值,我们只需一次遍历,就能将他们全部转化,剩下的共作就是每次对比数值,输出对应字符串了。
PS:这道题在输入上坑了很久,原因在于%I64d 和 %lld ,用%I64d错在第三组,但是%lld是可以的。在网上查的资料也并没有很清楚之间的区别,但是如果用cin的话就不会遇到这种问题,会更加保险一些。这是网上的一点解释,附在这里:原文链接

I stiI believe the answer is related to %lld means long long decimal, which isn’t guaranteed to be 64-bit. It could be, for example 128 bit on some systems. (Although if the variable is long long rather than, say, uint64_t, then I expect %lld is the right thing to use - or it would go wrong the other way around)
Unfortunately, the design of printf and scanf and their siblings is such that the compiler implementation and the format must match.
Obviously, cout and cin are safe in the sense that the compiler will chose the right output and input translation in itself.
It may also have something to do with what compiler(s) the “online judges” use - I think Microsoft compilers at some point supported 64-bit integers, but not long long, and thus didn’t have %lld, but did have %l64d.

代码实现:

/*
ID: eashion
LANG: C++
TASK: namenum
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#define MAX 5050
#define LL long long

using namespace std;

struct lis{
    char cow[20];
    LL num;
};

LL N;
int num;
bool flag;
map<char,int> mm;
lis Nlis[MAX];
void preope(FILE *dict);
int main()
{
    freopen("namenum.in","r",stdin);
    freopen("namenum.out","w",stdout);
    FILE *dict = fopen("dict.txt","r");
    preope(dict);
    fclose(dict);
    while( scanf("%lld",&N) != EOF ){
        flag = false;
        for( int i = 0; i < num; i++ ){
            if( N == Nlis[i].num ){
                cout<<Nlis[i].cow;
                flag = true;
            }
        }
        if( flag == false ){
            cout<<"NONE"<<endl;
        }
    }
    return 0;
}

void preope(FILE *dict){
    num = 0;
    char tmp[20];
    mm['A'] = mm['B'] = mm['C'] = 2;
    mm['D'] = mm['E'] = mm['F'] = 3;
    mm['G'] = mm['H'] = mm['I'] = 4;
    mm['J'] = mm['K'] = mm['L'] = 5;
    mm['M'] = mm['N'] = mm['O'] = 6;
    mm['P'] = mm['R'] = mm['S'] = 7;
    mm['T'] = mm['U'] = mm['V'] = 8;
    mm['W'] = mm['X'] = mm['Y'] = 9;
    while( fgets(tmp,20,dict) ){
        LL value=0;
        for( int i = 0; tmp[i] != '\n' && tmp[i] != '\0'; i++ ){
            value *= 10;
            value += mm[tmp[i]];
        }
        strcpy(Nlis[num].cow,tmp);
        Nlis[num].num = value;
        num++;
    }
    return ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值