64,654
社区成员
发帖
与我相关
我的任务
分享
// 凑MakeValue,默认凑14. 其中:OutPutArray:输出结果,这个数组存放的数据,相当于压缩数据,
// 例如OutPutArray[6] = 10,表示6的个数有10,优点是,如果数很多可以省内存。
bool MakeAndDelete(const int *InputArray, int count, int *OutPutArray, int MakeValue = 14)
{
ASSERT(NULL != InputArray && NULL != OutPutArray)
memset(OutPutArray,0,MakeValue*sizeof(int));// 输出清0
for (int i = 0; i < count; i++) // 哈,统计数目的同时,实际上就完了排序
{
if (InputArray[i]<1 || InputArray[i] >= MakeValue)// 防止输入值无效 {
return false;
}
OutPutArray[ InputArray[i] ] += 1;
for (int i = 1; i < MakeValue/2; i++) // 删除两数之和为14的
{
if (OutPutArray[i] > OutPutArray[MakeValue - i])
{
OutPutArray[i] = OutPutArray[i] - OutPutArray[MakeValue - i];
OutPutArray[MakeValue - i] = 0; // 注意前后不能换
}
else
{
OutPutArray[MakeValue - i] = OutPutArray[MakeValue - i] - OutPutArray[i];
OutPutArray[i] = 0; // 注意前后不能换
}
}
if(MakeValue%2 == 0)
{
OutPutArray[MakeValue/2] = OutPutArray[MakeValue/2]%2;
}
return true;
}
#include <iostream>
#include <set>
using namespace std;
int main()
{
bool b_erase = false, b_even = false;
multiset<int> coll, coll_copy;
copy(istream_iterator<int>(cin), istream_iterator<int>(), inserter(coll, coll.begin()));
multiset<int>::size_type count7 = coll.count(7);
if (count7 > 0)
{
if (count7 % 2 == 0)
{
b_even = true;
}
coll.erase(7);
}
copy(coll.begin(), coll.end(), inserter(coll_copy, coll_copy.begin()));
multiset<int>::iterator pos = coll.begin();
multiset<int>::iterator pos_copy;
for (; pos != coll.end();)
{
for (pos_copy = coll_copy.begin(); pos_copy != coll_copy.end(); ++pos_copy)
{
if (*pos + *pos_copy == 14)
{
b_erase = true;
coll.erase(pos++);
coll_copy.erase(pos_copy);
break;
}
}
if (!b_erase)
{
++pos;
}
else
{
b_erase = false;
}
}
if (!b_even)
{
coll.insert(7);
}
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
system("PAUSE");
}
http://hi.csdn.net/attachment/201106/16/3408116_1308196803TPCt.jpg
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> coll, coll_copy;
copy(istream_iterator<int>(cin), istream_iterator<int>(), inserter(coll, coll.begin()));
if (coll.count(7) > 0)
{
coll.erase(7);
}
copy(coll.begin(), coll.end(), inserter(coll_copy, coll_copy.begin()));
multiset<int>::iterator pos = coll.begin();
multiset<int>::iterator pos_copy;
bool b_erase = false;
for (; pos != coll.end();)
{
for (pos_copy = coll_copy.begin(); pos_copy != coll_copy.end(); ++pos_copy)
{
if (*pos + *pos_copy == 14)
{
b_erase = true;
coll.erase(pos++);
coll_copy.erase(pos_copy);
break;
}
}
if (!b_erase)
{
++pos;
}
else
{
b_erase = false;
}
}
coll.insert(7);
copy(coll.begin(), coll.end(), ostream_iterator<int>(cout, " "));
system("PAUSE");
}