zoukankan      html  css  js  c++  java
  • Hardwood Species(stl map)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=203#problem/B

    属于暴力

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <queue>
    #include <string>//string在这个头文件里;
    #include <map>//map在这个头文件里;
    #include <iostream>//cout在这个头文件里;
    int cmp(const void *a,const void *b)
    {
        return strcmp((char *)a,(char *)b);
    }
    using namespace std;
    char a[1000001][31];
    int main()
    {
        int i=0;
        map<string,int>q;
        //q.clear();
        while(gets(a[i])!=NULL)
        {
            i++;
        }
        qsort(a,i,sizeof(a[0]),cmp);
        for(int j=0;j<i;j++)
        {
            q[a[j]]++;
        }
        float rr;
        rr=q[a[0]]*100.0/i;
        printf("%s %.4f
    ",a[0],rr);
        for(int j=1;j<i;j++)
        {
            if(strcmp(a[j],a[j-1])!=0)
            {
                 rr=q[a[j]]*100.0/i;
                 printf("%s %.4f
    ",a[j],rr);
            }
        }
        return 0;
    }

     现在STL刚入门

    #include <string.h>
    //#include <stdlib.h>//加了它编译错误
    #include <stdio.h>
    #include <string>
    #include <map>
    using namespace std;
    
    int main()
    {
        int tt=0;
        char a[100];
        double sum;
        map<string,int>q;
        map<string,int>::iterator it;
        while(gets(a)!=NULL)
        {
            q[a]++;
            tt++;
        }
        for(it=q.begin();it!=q.end();it++)
        {
            sum=100.0*((double)it->second/((double)tt));
            printf("%s %.4lf
    ",it->first.data(),sum);
        }
        return 0;
    }

     这是学长以前写的

    这个代码是我第一次写的,不知道为什么 刚开始用了很多头文件,G++ wrong,  c++ 编译错误

    现在是 改了头文件后的 AC代码

    复制代码
     1 #include<cstdio>
     2  #include<string>
     3  #include<iostream>
     4  #include<map>
     5  using namespace std;
     6  
     7  char s[100];
     8  int main()
     9  {
    10      map<string,int>mp;
    11      map<string,int>::iterator iter;
    12      int i,sum=0;
    13      while(gets(s)!=NULL)
    14      {
    15          mp[s]++;
    16          sum++;
    17      }
    18      iter=mp.begin();
    19      while(iter!=mp.end())
    20      {
    21          cout<<iter->first;
    22          printf(" %.4lf
    ",100*1.0*iter->second/sum);
    23          iter++;
    24      }
    25      return 0;
    26  }
    27  
    复制代码
  • 相关阅读:
    SSM添加数据后自动获取ID
    EasyUI分页
    JavaScript增强AJAX基础
    高德地图MapAPI地图展示
    项目json代码
    JavaScript 事件机制
    JavaScript event flow
    java和JavaScript的区别
    history of program
    javaScript obj
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/3894051.html
Copyright © 2011-2022 走看看