#pragma warning (disable : 4786) // おまじない #include #include #include #include #include #include using namespace std; #define for_all(c,i) for (i=c.begin();i!=c.end();++i) #define all(c) c.begin(), c.end() typedef map CHeightMap; struct CPerson { bool m_bMale; // true:男, false:女 int m_height; // 身長(cm) int m_weight; // 体重(kg) int m_shoe; // 靴サイズ(mm) }; int ReadFile(const char* filename,vector& out) { FILE* f = fopen(filename,"r"); if (!f) return -1; char buf[256],c; CPerson p; double d; out.clear(); while (fgets(buf,sizeof(buf),f)) { if (sscanf(buf,"%*s %c %d %d %lf", &c,&p.m_height,&p.m_weight,&d) != 4) continue; p.m_bMale = c == 'M'; p.m_shoe = int(floor(d * 10 + .5)); out.push_back(p); } fclose(f); return 0; } struct FCompHeight { bool operator()(const CPerson& i,const CPerson& j)const{ return i.m_height < j.m_height; } }; struct FCompWeight { bool operator()(const CPerson& i,const CPerson& j)const{ return i.m_weight < j.m_weight; } }; struct FCompShoe { bool operator()(const CPerson& i,const CPerson& j)const{ return i.m_shoe < j.m_shoe; } }; struct FCompHeightWeight { bool operator()(const CPerson& i,const CPerson& j)const{ return i.m_height < j.m_height || (i.m_height == j.m_height && i.m_weight < j.m_weight); } }; struct FCompMale { bool operator()(const CPerson& i)const{return i.m_bMale;} }; void Print(vector::iterator b,vector::iterator e) { vector::iterator it; printf("Max Height %d Weight %d Shoe %d\n", max_element(b,e,FCompHeight())->m_height, max_element(b,e,FCompWeight())->m_weight, max_element(b,e,FCompShoe())->m_shoe); printf("Min Height %d Weight %d Shoe %d\n", min_element(b,e,FCompHeight())->m_height, min_element(b,e,FCompWeight())->m_weight, min_element(b,e,FCompShoe())->m_shoe); int k = 0; /* printf("Input k > "); scanf("%d",&k); assert(k >= 0 && k < distance(b,e)); nth_element(b,b+k,e,FCompHeight()); printf("%d th Height = %d\n",k,(b+k)->m_height); */ sort(b,e,FCompHeightWeight()); k = 0; for (it=b;it!=e;++it) { printf("%s %d %d %d ",it->m_bMale ? "男" : "女", it->m_height,it->m_weight,it->m_shoe); if (++k%5 == 0) printf("\n"); } printf("\n"); CHeightMap mp; CHeightMap::iterator mit; for (it=b;it!=e;++it) ++mp[it->m_height]; k = 0; for_all(mp,mit) { printf("%d: %d ",mit->first,mit->second); if (++k%10 == 0) printf("\n"); } printf("\n"); } int main() { vector data; vector::iterator it; if (ReadFile("data.txt",data)) return 1; // Print(all(data)); it = partition(all(data),FCompMale()); printf("男だけ\n"); Print(data.begin(),it); printf("女だけ\n"); Print(it,data.end()); return 0; }