Sunday, August 6, 2017

UVa problem solution 10226 - Hardwood Species

problem link:

Discuss: IN this problem you can work with map.just simply take input with map.don't worry about ordering because map is ordered string set.take input string if the length of string is zero then you can stop take input. then just iterate map for sum the second element then in next iterator print the first string of map then percentage the coverage of second element of map. then just print that.


try your self before see the code



#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        scanf("\n");
        map<string,int>mp;
        char str[100000];
        int i,j,cnt=0,flag=0,sum=0;
        double cover;
        while(gets(str))
            {
                if(strlen(str)==0)
                    break;
                mp[str]++;
            }
            for(map<string,int>:: const_iterator it=mp.begin();it!=mp.end();++it)
            {
                sum+=it->second;
            }
            for(map<string,int>::const_iterator it=mp.begin();it!=mp.end();++it)
            {
                cout <<it->first<<' ';
                cover=((double)it->second/(double)sum)*100.0;
               printf("%.4lf\n",cover);
            }
            if(t>0)
                cout <<endl;
    }
    return 0;
}

No comments:

Post a Comment