判断是否是栈的弹出序列
bool IsPop(char *PopOrder, char *PushOrder,int len)
{
if (PopOrder == nullptr || PushOrder == nullptr || len <= 0)
return false;
bool Possible = false;
char *PopStart = PopOrder;
char *PushStart = PushOrder;
stack<int> pStack;
while (PopStart - PopOrder < len)
{
while (pStack.empty() || pStack.top() != *PopOrder)
{
if (PushStart - PushOrder >= len)
break;
pStack.push(*PushStart);
PushStart++;
}
if (pStack.top() != *PopOrder)
break;
pStack.pop();
PopStart++;
}
if (pStack.empty() && PopStart - PopOrder == len)
Possible = true;
return Possible;
}
二进制中一的个数
//#include<iostream>
//#include<stack>
//using namespace std;
//bool IsPop(char *PopOrder, char *PushOrder,int len)
//{
// if (PopOrder == nullptr || PushOrder == nullptr || len <= 0)
// return false;
// bool Possible = false;
// char *PopStart = PopOrder;
// char *PushStart = PushOrder;
// stack<int> pStack;
// while (PopStart - PopOrder < len)
// {
// while (pStack.empty() || pStack.top() != *PopOrder)
// {
// if (PushStart - PushOrder >= len)
// break;
// pStack.push(*PushStart);
// PushStart++;
// }
// if (pStack.top() != *PopOrder)
// break;
// pStack.pop();
// PopStart++;
//
// }
// if (pStack.empty() && PopStart - PopOrder == len)
// Possible = true;
// return Possible;
//}
//int main()
//{
//
//}
#include<iostream>
using namespace std;
int getNum(int n)
{
int count = 0;
while (n)
{
n &= (n - 1);
++count;
}
return count;
}
int main()
{
int n;
while (cin >> n)
cout << getNum(n) << endl;
return 0;
}