程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> USACO Name That Number(File)

USACO Name That Number(File)

編輯:關於C++

  題解:

題目應該不難理解,就是找出數字對應字典中的字符串。因為數字對字符是一對多的關系,所以輸入的數字可能對應多個答案。如果每次都要一一對比查找的話顯然太浪費時間。轉換思路,對於字典中的字符串,他們都有自己唯一對應的數值,我們只需一次遍歷,就能將他們全部轉化,剩下的共作就是每次對比數值,輸出對應字符串了。
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 
#include 
#include 
#include 
#include
#define MAX 5050
#define LL long long

using namespace std;

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

LL N;
int num;
bool flag;
map 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<

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved