64,650
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
int Elem[100002];
int length;
}COutPut;
int Partition(COutPut &L, int low, int high)
{
// 交换排序表L中字表Elem[low...high]的记录,枢轴记录到位,并返回其所在位置,此时在它之前(后)的记录均不大(小)于它
L.Elem[0] = L.Elem[low]; // 用子表的第一个记录作枢轴记录
int pivokey = L.Elem[low];
while (low < high)
{
// 从表的两端交替地向中间扫描
while (low < high && L.Elem[high] >= pivokey)
{
--high;
}
L.Elem[low] = L.Elem[high]; // 将比枢轴记录小的记录移到低端
while (low < high && L.Elem[low] <= pivokey)
{
++low;
}
L.Elem[high] = L.Elem[low]; // 将比枢轴记录大的记录移到高端
}
L.Elem[low] = L.Elem[0]; // 枢轴记录到位
return low;
}
void QSort(COutPut &L, int low, int high)
{
// 对顺序表L中的子序列L.Elem[low...high]作快速排序
int pivotloc;
if (low < high)
{
// 长度大于1
pivotloc = Partition(L, low, high); // 将L.Elem[low...high]一分为二
QSort(L, low, pivotloc - 1); // 对低子表递归排序,pivotloc是枢轴位置
QSort(L, pivotloc + 1, high); //对高字表递归排序
}
}
int main()
{
// ASCII码,0~9对应48~57,A~P对应65~80,R~Y对应82~89,-为45
// 这道题的数值判断感觉可以用ASCII码会^^高级一点- -。
// 第一遍的思路:输入一个就去前面搜。
// 超时,肯定的。
// 新的思路:插入时不判断重复,全部输入完成后,进行快排,然后输出
// 第二遍时输出的时候超时了,因为我是把int通过strstream转成了string然后补0加-输出,很耗时
int m_Count;
cin >> m_Count;
// 大于100000报错
if (m_Count > 100000)
{
cout << "Input is too large!" << endl;
system("pause");
return 0;
}
else if (m_Count < 1)
{
cout << "Input is too small!" << endl;
system("pause");
return 0;
}
// 用来控制输入的字符串数量等于m_Count的临时变量
int m_CountTemp = 0;
// 用来保存输入的字符串,处理过后的,因为是一个七位数,int是能存下的
COutPut m_StrDealed_ToInt;
// 用来接收每一次输入的字符串然后处理的字符串变量
string m_StrInput;
// 循环str的临时变量
int m_StrCycle_Temp;
// 用来计算每个字符串中的字符除掉-后是否为7个
int m_NumCount;
// 找到'-'位置的临时变量
int m_Pos = 0;
// 循环m_StrDealed_ToInt的临时变量
int m_IntCycle_Temp;
// 用来存储接收到的字符串所变成的Int的临时变量
int m_StrDealed_ToInt_Temp;
// 用来标识真正加入到COutPut里的数据的数目的临时变量
int m_ValueCount_Temp = 0;
// 输出时重复的数据的计数
int m_RepeatCount = 1;
// 用来标识是否有重复数据
bool b_RepeatExist;
// 初始化,0位作排序标志位,length最开始置0
m_StrDealed_ToInt.length = 0;
m_StrDealed_ToInt.Elem[0] = 0;
// 输入
while ((m_CountTemp < m_Count) && (cin >> m_StrInput))
{
// 去掉所有的'-'
m_Pos = m_StrInput.find('-');
while (m_Pos != -1)
{
m_StrInput = m_StrInput.substr(0, m_Pos) + m_StrInput.substr(m_Pos + 1, m_StrInput.size());
m_Pos = m_StrInput.find('-');
}
m_NumCount = 0;
// 转换并计数
for (m_StrCycle_Temp = 0; m_StrCycle_Temp < m_StrInput.size(); m_StrCycle_Temp++)
{
if ( (m_StrInput[m_StrCycle_Temp] == 'A') || (m_StrInput[m_StrCycle_Temp] == 'B') || (m_StrInput[m_StrCycle_Temp] == 'C'))
{
m_StrInput[m_StrCycle_Temp] = '2';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'D') || (m_StrInput[m_StrCycle_Temp] == 'E') || (m_StrInput[m_StrCycle_Temp] == 'F'))
{
m_StrInput[m_StrCycle_Temp] = '3';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'G') || (m_StrInput[m_StrCycle_Temp] == 'H') || (m_StrInput[m_StrCycle_Temp] == 'I'))
{
m_StrInput[m_StrCycle_Temp] = '4';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'J') || (m_StrInput[m_StrCycle_Temp] == 'K') || (m_StrInput[m_StrCycle_Temp] == 'L'))
{
m_StrInput[m_StrCycle_Temp] = '5';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'M') || (m_StrInput[m_StrCycle_Temp] == 'N') || (m_StrInput[m_StrCycle_Temp] == 'O'))
{
m_StrInput[m_StrCycle_Temp] = '6';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'P') || (m_StrInput[m_StrCycle_Temp] == 'R') || (m_StrInput[m_StrCycle_Temp] == 'S'))
{
m_StrInput[m_StrCycle_Temp] = '7';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'T') || (m_StrInput[m_StrCycle_Temp] == 'U') || (m_StrInput[m_StrCycle_Temp] == 'V'))
{
m_StrInput[m_StrCycle_Temp] = '8';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'W') || (m_StrInput[m_StrCycle_Temp] == 'X') || (m_StrInput[m_StrCycle_Temp] == 'Y'))
{
m_StrInput[m_StrCycle_Temp] = '9';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] >= '0') && (m_StrInput[m_StrCycle_Temp] <= '9'))
{
// 0~9
m_NumCount++;
}
else
{
cout << "Input is not in (A~P && 0~9 && -). No Accepted!" << endl;
system("pause");
return 0;
}
}
// 如果长度不为7,不接受
if (m_NumCount != 7)
{
cout << "The num of the characters (not inclued '-') in string is not 7. No Accepted!" << endl;
system("pause");
return 0;
}
// 将该字符串转换为Int
m_StrDealed_ToInt_Temp = atoi(m_StrInput.c_str());
m_StrDealed_ToInt.Elem[m_CountTemp+1] = m_StrDealed_ToInt_Temp;
m_StrDealed_ToInt.length++;
// 这是用于计算输入的字符串个数的
m_CountTemp++;
}
// 快速排序
QSort(m_StrDealed_ToInt, 1, m_StrDealed_ToInt.length);
// 输出
b_RepeatExist = false;
for (m_IntCycle_Temp = 1; m_IntCycle_Temp <= m_StrDealed_ToInt.length; m_IntCycle_Temp++)
{
if (m_StrDealed_ToInt.Elem[m_IntCycle_Temp+1] != m_StrDealed_ToInt.Elem[m_IntCycle_Temp])
{
// 有输出了就改为true
if (m_RepeatCount != 1)
{
if (!b_RepeatExist)
{
b_RepeatExist = true;
}
// 输出
printf("%.3d-%.4d %d\n", m_StrDealed_ToInt.Elem[m_IntCycle_Temp]/10000, m_StrDealed_ToInt.Elem[m_IntCycle_Temp]%10000, m_RepeatCount);
}
m_RepeatCount = 1;
}
else
{
m_RepeatCount++;
}
}
if (!b_RepeatExist)
{
cout << "No duplicates." << endl;
}
system("pause");
return 1;
}
for (m_StrCycle_Temp = 0; m_StrCycle_Temp < m_StrInput.size(); m_StrCycle_Temp++)
{
if ( (m_StrInput[m_StrCycle_Temp] == 'A') || (m_StrInput[m_StrCycle_Temp] == 'B') || (m_StrInput[m_StrCycle_Temp] == 'C'))
{
m_StrInput[m_StrCycle_Temp] = '2';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'D') || (m_StrInput[m_StrCycle_Temp] == 'E') || (m_StrInput[m_StrCycle_Temp] == 'F'))
{
m_StrInput[m_StrCycle_Temp] = '3';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'G') || (m_StrInput[m_StrCycle_Temp] == 'H') || (m_StrInput[m_StrCycle_Temp] == 'I'))
{
m_StrInput[m_StrCycle_Temp] = '4';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'J') || (m_StrInput[m_StrCycle_Temp] == 'K') || (m_StrInput[m_StrCycle_Temp] == 'L'))
{
m_StrInput[m_StrCycle_Temp] = '5';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'M') || (m_StrInput[m_StrCycle_Temp] == 'N') || (m_StrInput[m_StrCycle_Temp] == 'O'))
{
m_StrInput[m_StrCycle_Temp] = '6';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'P') || (m_StrInput[m_StrCycle_Temp] == 'R') || (m_StrInput[m_StrCycle_Temp] == 'S'))
{
m_StrInput[m_StrCycle_Temp] = '7';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'T') || (m_StrInput[m_StrCycle_Temp] == 'U') || (m_StrInput[m_StrCycle_Temp] == 'V'))
{
m_StrInput[m_StrCycle_Temp] = '8';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'W') || (m_StrInput[m_StrCycle_Temp] == 'X') || (m_StrInput[m_StrCycle_Temp] == 'Y'))
{
m_StrInput[m_StrCycle_Temp] = '9';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] >= '0') && (m_StrInput[m_StrCycle_Temp] <= '9'))
{
// 0~9
m_NumCount++;
}
else
{
cout << "Input is not in (A~P && 0~9 && -). No Accepted!" << endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
typedef struct
{
int Elem[100002];
int length;
}COutPut;
double random(double start, double end)
{
return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
int Partition(COutPut &L, int low, int high)
{
// 交换排序表L中字表Elem[low...high]的记录,枢轴记录到位,并返回其所在位置,此时在它之前(后)的记录均不大(小)于它
srand(unsigned(time(0)));
int m_Random = random(low,high);
L.Elem[0] = L.Elem[m_Random]; // 用随机出的记录作枢轴记录
int pivokey = L.Elem[m_Random];
int temp;
temp = L.Elem[m_Random];
L.Elem[m_Random] = L.Elem[low];
L.Elem[low] = L.Elem[m_Random];
while (low < high)
{
// 从表的两端交替地向中间扫描
while (low < high && L.Elem[high] >= pivokey)
{
--high;
}
L.Elem[low] = L.Elem[high]; // 将比枢轴记录小的记录移到低端
while (low < high && L.Elem[low] <= pivokey)
{
++low;
}
L.Elem[high] = L.Elem[low]; // 将比枢轴记录大的记录移到高端
}
L.Elem[low] = L.Elem[0]; // 枢轴记录到位
return low;
}
void QSort(COutPut &L, int low, int high)
{
// 对顺序表L中的子序列L.Elem[low...high]作快速排序
int pivotloc;
if (low < high)
{
// 长度大于1
pivotloc = Partition(L, low, high); // 将L.Elem[low...high]一分为二
QSort(L, low, pivotloc - 1); // 对低子表递归排序,pivotloc是枢轴位置
QSort(L, pivotloc + 1, high); //对高字表递归排序
}
}
int main()
{
int m_Count;
cin >> m_Count;
// 用来控制输入的字符串数量等于m_Count的临时变量
int m_CountTemp = 0;
// 用来保存输入的字符串,处理过后的,因为是一个七位数,int是能存下的
COutPut m_StrDealed_ToInt;
// 用来接收每一次输入的字符串然后处理的字符串变量
string m_StrInput;
// 循环str的临时变量
int m_StrCycle_Temp;
// 用来计算每个字符串中的字符除掉-后是否为7个
int m_NumCount;
// 找到'-'位置的临时变量
int m_Pos = 0;
// 循环m_StrDealed_ToInt的临时变量
int m_IntCycle_Temp;
// 用来存储接收到的字符串所变成的Int的临时变量
int m_StrDealed_ToInt_Temp;
// 用来标识真正加入到COutPut里的数据的数目的临时变量
int m_ValueCount_Temp = 0;
// 输出时重复的数据的计数
int m_RepeatCount = 1;
// 用来标识是否有重复数据
bool b_RepeatExist;
// 初始化,0位作排序标志位,length最开始置0
m_StrDealed_ToInt.length = 0;
m_StrDealed_ToInt.Elem[0] = 0;
// 输入
while ((m_CountTemp < m_Count) && (cin >> m_StrInput))
{
// 去掉所有的'-'
m_Pos = m_StrInput.find('-');
while (m_Pos != -1)
{
m_StrInput = m_StrInput.substr(0, m_Pos) + m_StrInput.substr(m_Pos + 1, m_StrInput.size());
m_Pos = m_StrInput.find('-');
}
m_NumCount = 0;
// 转换并计数
for (m_StrCycle_Temp = 0; m_StrCycle_Temp < m_StrInput.size(); m_StrCycle_Temp++)
{
if ( (m_StrInput[m_StrCycle_Temp] == 'A') || (m_StrInput[m_StrCycle_Temp] == 'B') || (m_StrInput[m_StrCycle_Temp] == 'C'))
{
m_StrInput[m_StrCycle_Temp] = '2';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'D') || (m_StrInput[m_StrCycle_Temp] == 'E') || (m_StrInput[m_StrCycle_Temp] == 'F'))
{
m_StrInput[m_StrCycle_Temp] = '3';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'G') || (m_StrInput[m_StrCycle_Temp] == 'H') || (m_StrInput[m_StrCycle_Temp] == 'I'))
{
m_StrInput[m_StrCycle_Temp] = '4';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'J') || (m_StrInput[m_StrCycle_Temp] == 'K') || (m_StrInput[m_StrCycle_Temp] == 'L'))
{
m_StrInput[m_StrCycle_Temp] = '5';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'M') || (m_StrInput[m_StrCycle_Temp] == 'N') || (m_StrInput[m_StrCycle_Temp] == 'O'))
{
m_StrInput[m_StrCycle_Temp] = '6';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'P') || (m_StrInput[m_StrCycle_Temp] == 'R') || (m_StrInput[m_StrCycle_Temp] == 'S'))
{
m_StrInput[m_StrCycle_Temp] = '7';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'T') || (m_StrInput[m_StrCycle_Temp] == 'U') || (m_StrInput[m_StrCycle_Temp] == 'V'))
{
m_StrInput[m_StrCycle_Temp] = '8';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] == 'W') || (m_StrInput[m_StrCycle_Temp] == 'X') || (m_StrInput[m_StrCycle_Temp] == 'Y'))
{
m_StrInput[m_StrCycle_Temp] = '9';
m_NumCount++;
}
else if ( (m_StrInput[m_StrCycle_Temp] >= '0') && (m_StrInput[m_StrCycle_Temp] <= '9'))
{
// 0~9
m_NumCount++;
}
}
// 将该字符串转换为Int
m_StrDealed_ToInt_Temp = atoi(m_StrInput.c_str());
m_StrDealed_ToInt.Elem[m_CountTemp+1] = m_StrDealed_ToInt_Temp;
m_StrDealed_ToInt.length++;
// 这是用于计算输入的字符串个数的
m_CountTemp++;
}
// 快速排序
QSort(m_StrDealed_ToInt, 1, m_StrDealed_ToInt.length);
// 输出
b_RepeatExist = false;
for (m_IntCycle_Temp = 1; m_IntCycle_Temp <= m_StrDealed_ToInt.length; m_IntCycle_Temp++)
{
if (m_StrDealed_ToInt.Elem[m_IntCycle_Temp+1] != m_StrDealed_ToInt.Elem[m_IntCycle_Temp])
{
// 有输出了就改为true
if (m_RepeatCount != 1)
{
if (!b_RepeatExist)
{
b_RepeatExist = true;
}
// 输出
printf("%.3d-%.4d %d\n", m_StrDealed_ToInt.Elem[m_IntCycle_Temp]/10000, m_StrDealed_ToInt.Elem[m_IntCycle_Temp]%10000, m_RepeatCount);
}
m_RepeatCount = 1;
}
else
{
m_RepeatCount++;
}
}
if (!b_RepeatExist)
{
cout << "No duplicates." << endl;
}
system("pause");
return 1;
}