小红花STL

要求:幼儿园中按班组织学生,每个班学生按学号排号,规模为25个孩子,学校每天出勤用一个bitset描述,1代表出勤,0代表缺勤,试着编写接口函数,

输出每个孩子序号与出勤数

找出出勤率最高的3个孩子给当月的小红花。

数据:

1111111111111111111111111

1110111011101110111011100

1101101101101101101101101

1111011110111101111011110

0011101110111101111101111

0011101111011111110111111

1111101111110111111100111

1100111110001111111111110

1111111111111101111111111

1111011111011111111110111

 

#include<iostream>
#include<vector>
#include<bitset>
#include<algorithm>
#include<fstream>
#include<string>
#include<iterator>
using namespace std;
template<size_t N>
class base
{
public:
	bitset<N> b;
	int id;

};

template<size_t N>
class com
{
public:
	com() {}
	bool operator()(const base<N>& b1, const base<N>&b2)
	{
		return b1.b.count() <= b2.b.count() ? false : true;
	
	}
};


void main()
{
	vector<char>v;
	ifstream out("data.txt");
	copy(istream_iterator<char>(out), istream_iterator<char>(), back_inserter(v));
	vector<base<10>> vb(25);
	cout << endl;
	cout << endl;	cout << endl;
	for (int i=0;i<25;i++)
	{
		for (int j =0;j<10;j++)
		{
			vb[i].b.set(j, (v[j * 25 + i]-48));
		}
		vb[i].id = i;
		cout << vb[i].b.to_string() << endl;
	}
	for (int i=0;i<25;i++)
	{
		cout << vb[i].b.count() << endl;
	}
	sort(vb.begin(),vb.end(),com<10>());
	for (int i = 0; i < 25; i++)
	{
		cout << vb[i].b.to_string() <<"   "<<vb[i].id<< endl;
	}
	cout << "小红花的同学是" << endl;
	cout << vb[0].id <<"    "<< vb[1].id << "    " << vb[2].id << "   " <<vb[3].id<<endl;

	
	cin.get();
}

测试结果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值