首页 > 代码库 > ZOJ 1755 && POJ 1547 Clay Bully(简单排序+map容器)

ZOJ 1755 && POJ 1547 Clay Bully(简单排序+map容器)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1755

题意:Ms. Terry is a pre-school art teacher who likes to have her students work with clay. One of her assignments is to form a lump of clay into a block and then measure the dimensions of the block. However, in every class, there is always one child who insists on taking some clay from some other child. Since Ms. Terry always gives every child in a class the same amount of clay to begin with, you can write a program that helps Ms. Terry find the bully and victim after she measures each child‘s finished block.

思路:排序,求最小和最大的人的名字。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int Inf=0x3f3f3f3f;
const int maxn=1000;
const double eps=1e-6;
const double pi=acos(-1.0);
int n,m,i,j;
int cost[maxn][maxn];
double s(double x,double y)
{
    return ((x*x+y*y)*pi)/2.0;
}
struct node
{
    int a,b,c;
    string str;
} aa[250];
bool cmp(node zz, node cc)
{
    return zz.a*zz.b*zz.c<=cc.a*cc.b*cc.c;
}
int bb[maxn];
int main()
{
    int ncase,i,j,t,n,m;
    while(cin>>ncase&&ncase!=-1)
    {
        for(i=0;i<ncase;i++)
        {
            cin>>aa[i].a>>aa[i].b>>aa[i].c>>aa[i].str;
                  //bb[i]=aa[i].a*aa[i].b*aa[i].c;
        }
        sort(aa,aa+ncase,cmp);
        cout<<aa[ncase-1].str<<" "<<"took clay from"<<" "<<aa[0].str<<"."<<endl;
    }
}


STL 的MAP :

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
    map<int ,string > qq;
    int i,j,n,m,a,b,c;
    string name;
    while(1)
    {
        cin>>n;
        if(n==-1) break;
        qq.clear();
        for(i=0; i<n; i++)
        {
            cin>>a>>b>>c>>name;
            qq.insert(map<int ,string>::value_type(a*b*c,name));
        }
        cout<<qq.rbegin()->second+" took clay from "+qq.begin()->second+"."<<endl;
    }
}


ZOJ 1755 && POJ 1547 Clay Bully(简单排序+map容器)