NOJ1024 01排序

和1024几乎一样,我甚至只是改了几个判断字符就AC了。

从侧面证明我可能写了一个通用型判断程序

 include<iostream>
include<vector>
include<string>
include<algorithm>
using namespace std;
struct CC {
string str;
int firstposition = 0;
int amount = 0;
void countA();
void positionA();
bool operator<(const CC& A) const
{
if (str.size() < A.str.size()) //检测字符串长度
return true;
else if (str.size() > A.str.size())
return false;
if (amount < A.amount) //检测1的数量
return true;
else if (amount > A.amount)
return false;
if (firstposition < A.firstposition) //检测ASCII码值?
return true;
else if (firstposition > A.firstposition)
return false;
return false;
}
};
void CC::countA()
{
int amount = 0;
for (auto t : this->str)
if (t == '1')
amount++;
this->amount = amount;
}
void CC::positionA()
{
int i = 0;
for (; i != (this->str).size(); i++)
if (str[i] == '1')
this->firstposition = i;
}
void print_each(const CC & c)
{
cout << c.str << endl; } int main() { vector list;
CC temp;
while (cin >> temp.str)
{
temp.countA();
temp.positionA();
list.push_back(temp);
}
sort(list.begin(), list.end());
for_each(list.begin(), list.end(), print_each);
return 0;
}

NOJ1024 01排序》有1个想法

发表评论

邮箱地址不会被公开。