zoukankan      html  css  js  c++  java
  • POJ 2418 Hardwood Species (哈希,%f 和 %lf)

      我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的)。当时我还以为是hash出错了。。

      方法不止一种:

      方法            时间        空间
      Hash           891ms      596k
      map<string,int>     2735ms      1316k
      sort            5000ms+    30000k+

      %lf 与 %f的具体区别:

      printf的%f说明符的确既可以输出float型又可以输出double型。根据“默认参数提升”规则float型会被提升为double型。因此printf()只会看到双精度数。对于scanf,情况就完全不同了,它接受指针,这里没有类似的类型提升。向float存储和向double存储大不一样,因此,scanf区别%f和%lf。 

      也就是说输出的时候不管输出的是双精度还是单精度都用%f就没错了,但是输入的时候,输入单精度要用%f而输入双精度要用%lf。

      哈希

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<sstream>
    using namespace std;
    struct Tree
    {
        char name[33];
        int sum;
    } tree[10005];
    int SDBMHash(char *str)
    {
        int Hash = 0;
        while(*str)
        {
            Hash = (*str++) + (Hash << 6) + (Hash << 16) - Hash;
        }
        return (Hash&0x7FFFFFFF);
    }
    bool cmp(Tree a,Tree b)
    {
        return strcmp(a.name,b.name)<0;
    }
    map<int,int> pos;
    int main()
    {
    //    freopen("H.in.cpp","r",stdin);
        char tmp[33];
        pos.clear();
        int tot = 0,all = 0;
        while(gets(tmp))
        {
            if(strcmp(tmp,"") == 0) break;
            all++;
            int H = SDBMHash(tmp);
            int id = pos[H];
            if(id == 0)
            {
                strcpy(tree[++tot].name,tmp);
                tree[tot].sum = 1;
                pos[H] = tot;
            }
            else
            {
                tree[id].sum++;
            }
        }
        sort(tree+1,tree+1+tot,cmp);
        for(int i = 1; i <= tot; i++)
        {
            printf("%s %.4f
    ",tree[i].name,tree[i].sum*100.0/all);
        }
        return 0;
    }
    View Code

      map<string,int>

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<sstream>
    using namespace std;
    struct Tree
    {
        string name;
        int sum;
    } tree[10005];
    bool cmp(Tree a,Tree b)
    {
        return a.name < b.name;
    }
    map<string,int> vis;
    int main()
    {
    //    freopen("H.in.cpp","r",stdin);
        char a[33];
        string tmp;
        vis.clear();
        int tot = 0,all = 0;
        while(gets(a))
        {
            all++;
            int len = strlen(a);
            tmp = "";
            for(int i = 0; i < len; i++)
            {
                tmp += a[i];
            }
            int id = vis[tmp];
            if(id == 0)
            {
                tree[++tot].name = tmp;
                tree[tot].sum = 1;
                vis[tmp] = tot;
            }
            else
            {
                tree[id].sum++;
            }
        }
        sort(tree+1,tree+tot+1,cmp);
        for(int i = 1; i <= tot; i++)
        {
            cout<<tree[i].name<<" ";
            printf("%.4f
    ",tree[i].sum*100.0/all);
        }
        return 0;
    }
    View Code

      排序

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<sstream>
    using namespace std;
    struct Tree
    {
        char name[33];
    } tree[1000005];
    bool cmp(Tree a,Tree b)
    {
        return strcmp(a.name,b.name)<0;
    }
    int main()
    {
    //    freopen("H.in.cpp","r",stdin);
        int all = 0;
        while(gets(tree[all].name) && strlen(tree[all].name))
        {
            all++;
        }
        sort(tree,tree+all,cmp);
        int tot = 1;
        for(int i = 0; i < all; i++)
        {
            if(strcmp(tree[i].name,tree[i+1].name) == 0) tot++;
            else
            {
                printf("%s %.4f
    ",tree[i].name,tot*100.0/all);
                tot = 1;
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    完毕port(CompletionPort)具体解释
    [Java聊天室server]实战之五 读写循环(服务端)
    ImageMagick的安装及使用
    STL学习小结
    ORACLE中%TYPE和%ROWTYPE的使用
    命令模式在MVC框架中的应用
    代码阅读分析工具Understand 2.0试用
    SimpleDateFormat 的线程安全问题与解决方式
    C++垃圾回收机制
    TH文字编辑器开发的第一个游戏,唐伯虎泡妞
  • 原文地址:https://www.cnblogs.com/jifahu/p/5934180.html
Copyright © 2011-2022 走看看