5,530
社区成员
发帖
与我相关
我的任务
分享
#pragma once
#include<limits>
#include<vector>
#include<string>
#include<boost/lexical_cast.hpp>
#undef max
template<unsigned int BASE, class TYPE=unsigned>
class Enumerator
{
static_assert(BASE > 2, "template parameter should greater than 2!");
public:
// typedef unsigned int TYPE;
Enumerator(unsigned int count, TYPE stat = 0)
{
status.clear();
status.resize(count, stat);
}
bool operator ++()
{
int c = 1;
for (size_t i = 0 ; c != 0 && i < status.size() ; ++i)
{
status[i] += c;
if(status[i] >= BASE)
{
status[i] = 0;
c = 1;
}
else
{//不需要再继续进位
c = 0;
}
}
return c == 0;
}
bool operator++(int)
{
return this->operator++();
}
TYPE get(size_t i) const
{
if(i >= status.size())
throw std::out_of_range(std::string("i should less than ")
+ lexical_cast<string>(status.size()));
return status[i];
}
void set(size_t i, TYPE s)
{
if(i >= status.size())
throw std::out_of_range(string("i should less than ")
+ lexical_cast<string>(status.size()));
status[i] = s;
}
size_t count(TYPE s) const
{
size_t result = 0;
for (const auto& p : status)
{
if(p == s) result++;
}
return result;
}
size_t size() const
{
return status.size();
}
protected:
private:
std::vector<TYPE> status;
};
template<class T>
class Enumerator<2, T>
{
//只要是这种2状态的东西,直接采用unsigned类型,忽略类型参数T
typedef unsigned int TYPE;
public:
Enumerator(unsigned int count, bool statu = 0)
:SIZE(count)
{
status.clear();
status.resize(SIZE / BITS + 1, statu ? std::numeric_limits<TYPE>::max() : 0);
}
bool operator++()
{
static const auto MAX = std::numeric_limits<TYPE>::max();
int c = 1;
for (size_t i = 0 ; c != 0 && i < status.size() ; ++i)
{
if(status[i] == MAX)
{
status[i] = 0;
c = 1;
}
else
{
status[i]+=c;
c = 0;
}
}
return c == 0 && (status.back() >> (SIZE % BITS)) == 0;
}
bool operator++(int)
{
return operator++();
}
short get(size_t i) const
{
if(i >= SIZE) throw std::out_of_range(std::string("i should less than ") + boost::lexical_cast<std::string>(SIZE));
return (status[i / BITS] >> (i % BITS)) & 1;
}
void set(size_t i , bool s)
{
if(s)
status[i / BITS] |= 1 << (i % BITS);
else
status[i / BITS] &= ~(1 <<( i % BITS));
}
size_t count(bool s) const
{
unsigned result = 0;
for (auto p : status)
{
while(p)
{
result += p & 1;
p >>= 1;
}
}
return s ? result : SIZE - result;
}
size_t size() const
{
return SIZE;
}
protected:
private:
static const auto BITS = std::numeric_limits<TYPE>::digits;
const size_t SIZE;
std::vector<TYPE> status;
};
#include "Enumerator.h"
#include <iostream>
using std::cout;
using std::endl;
bool Check(const Enumerator<2>& e)
{
assert(e.size() == 5);
const int A = 0;
const int B = 1;
const int C = 2;
const int D = 3;
const int E = 4;
//如果A参加,则B一定参加
if(e.get(A) && !e.get(B))
return false;
//C和D有且只有一人参加
if(e.get(C) == e.get(D)) return false;
//D和E状态一致
if(!(e.get(D) == e.get(E))) return false;
//如果C参加,则B不参加
if(e.get(C) && e.get(B)) return false;
//最多有两个人参加
if(e.count(true) > 2) return false;
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
Enumerator<2> d(5,0);
do
{
if(Check(d))
{
for (int i = 0 ; i <d.size() ; ++i)
{
cout<<(short)d.get(i);
}
cout<<endl;
}
} while (d++);
system("pause");
return 0;
}
#ifndef NOMINMAX
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#endif /* NOMINMAX */
#define NO_MINMAX
#include<cstdio>
using namespace std;
int pow(int a)
{
if(a==0)return 1;
else return 2*pow(a-1);
}
void HaveFun()
{
for(int i=0;i<=pow(6)-1;i++)
if(i&0x10?(i&0x8?1:0):1)
if(i&0x4 || i&0x2)
if(i&0x2 && i&0x1 || !(i&0x2 && i&0x1)){
int amount=2;
for(int offset=0;offset<=4;offset++)
if(i&1<<offset)amount--;
if(amount>=0)printf("%d\n",i);
}
}
int main()
{
HaveFun();
getchar();
return 0;
}
大概就是这个意思啦,不知道我有没有理解错题目意思。
您程序的代码风格不错啊,暂时没详细看,等我看完会给您提出建议的