cugb1046

来源:互联网 发布:如何创建网络平台 编辑:程序博客网 时间:2024/06/12 21:00
 

 排序水题 ,一开始变换一下数据,接着排序,没有重复的话,输出 No duplicates. 就因为这个,wa了两次,失败啊,

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string.h>
using namespace std;

const int Max = 100003;
struct Num
{
 int time;
 char str[10];
}num[Max];

void Rev(char ch[],int i)
{
 int j,len = 0;
 for(j = 0;j < strlen(ch);++j)
 {
  if(ch[j] >= 'A' && ch[j] <= 'Z')
  {
    int temp =  ch[j] - 'A';
    if(temp == 18 || temp == 21 || temp == 24)
     num[i].str[len++] = temp / 3 + 1 + '0';
    else num[i].str[len++] = temp / 3 + 2 + '0'; 
  }
  else if(ch[j] >= '0' && ch[j] <= '9')
   num[i].str[len++] = ch[j];
   
  if(len == 3) num[i].str[len++] = '-';
  else if(len >= 8) break;
 }
}

int Partition(int left,int right)
{
 Num temp = num[left];
 int i = left,j = right + 1;
 while(1)
 {
  while(strcmp(temp.str,num[++i].str) > 0 && i <= right);
  while(strcmp(temp.str,num[--j].str) < 0);
  if(i < j)
  {
      Num TEMP = num[i];
   num[i] = num[j];
   num[j] = TEMP;
  }
  else break;
 }
 
 num[left] = num[j];
 num[j] = temp;
 return j;
 
}

void Qsort(int left,int right)
{
 int mid;
 if(left < right)
 {
  mid = Partition(left,right);
  Qsort(left,mid - 1);
  Qsort(mid + 1,right);
 }
}

void print(int most)
{
 int i;
 for(i = 0;i < most;++i)
 {
  if(num[i].time > 1)
  { 
   cout << num[i].str << ' ' << num[i].time << endl;
  }
 }
 
}

int main(void)
{
 int most,i,j,len,flag = 0;
 char ch[20];
 
  cin >> most;
  for(i = 0;i < Max;++i) num[i].time = 1;
  for(i = 0;i < most;++i)
  {
   cin >> ch;
   Rev(ch,i);
  }
  
  Qsort(0,most - 1);
  
  j = 0;
  for(i = 1;i < most;++i)
  {
   if(!strcmp(num[i].str,num[j].str))
   {  
    num[j].time++;
   }
   else j = i;
  }
  
  for(i = 0;i < most ;++i) if(num[i].time > 1) flag = 1;
  
  if(flag) print(most);
  else cout << "No duplicates." << endl;
  

 
 system("pause");
 return 0;
}

 

原创粉丝点击