2 回答
TA贡献1863条经验 获得超2个赞
这段代码应该没问题。
#include<iostream>
using namespace std;
int a[200000],n;
void sort(int low,int high)
{
if(high-low<=9)//小优化,当序列小于9时,使用插入排序或选择排序可以减少少量的时间。我使用的是选择排序,被注释掉的那一大段代码是插入排序。
{
/*int i,j=low,k,tmp;
for(i=low+1;i<=high;i++)
{
j=low;
while(a[i]>a[j]&&j<i) j++;
if(j!=i)
{
tmp=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[k]=tmp;
}
}*/
int i,j;
for(i=low;i<high;i++)
for(j=i+1;j<=high;j++)
if(a[j]>a[i])
swap(a[i],a[j]);
return ;
}
int v=a[low],i=low,j=high+1;
do
{
do i++;
while(a[i]<v);
do j--;
while(a[j]>v);
if (i<j)
swap(a[i],a[j]);
}while(i<j);
a[low]=a[j];
a[j]=v;
sort(low,j-1);
sort(j+1,high);
return ;
}
int main()
{
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(0,n-1);
int tmp=a[0],s=1;
for(i=1;i<n;i++)
if(a[i]!=tmp)
{
printf("%d %d\n",tmp,s);
s=1;
tmp=a[i];
}
else
s++;
printf("%d %d\n",tmp,s);
return 0;
}
TA贡献1818条经验 获得超8个赞
你的程序,没工夫看,不过前几天做了道题,也用到了快排,下面是我的程序,给你学习学习,我的快排是用递归写的。用到了三个函数。。事实上c++库函数中自带有qsort函数,可直接调用的,你也可以学习qsort函数的用法
/* Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15011 Accepted: 6605
Description
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words.
Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word
appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line.
Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Huge input and output,scanf and printf are recommended.
Source
Waterloo local 2001.09.22
*/
#include <iostream>
using namespace std;
struct dic_entry
{
char en[11];//表示english;
char fo[11];//表示foreignlanguage
} d[100001];//d表示dictionary; //真奇怪,如果将d声明为main()的成员函数,数组就不允许开这么大,就会运行错误
void swap(int pos1,int pos2)
{
char temp1[11]="";
strcpy(temp1,d[pos1].en);
strcpy(d[pos1].en,d[pos2].en);
strcpy(d[pos2].en,temp1);
char temp2[11]="";
strcpy(temp2,d[pos1].fo);
strcpy(d[pos1].fo,d[pos2].fo);
strcpy(d[pos2].fo,temp2);
}
int partition(int low,int high)//这个函数返回的是pivot的位置
{
char pivot[11];
int i,last_small;
strcpy(pivot,d[low].fo);//
last_small=low;
for(i=low+1;i<=high;i++)
{
if(strcmp(d[i].fo,pivot)<0)
{
last_small=last_small+1;
swap(i,last_small);//交换两个位置的值
}
}
swap(last_small,low);//交换,将pivot放到它应该在的位置
//for(i=low;i<=high;i++)
return last_small;
}
void recursive_quick_sort(int low,int high)
{
int pivot_position; //支点的位置
if(low<high)
{
pivot_position=partition(low,high);//这个函数是用于寻找支点的位置
recursive_quick_sort(low,pivot_position-1);
recursive_quick_sort(pivot_position+1,high);
}
}
void QuickSort(int n)//按照fo字段进行排序
{
recursive_quick_sort(0,n-1);
}
void BinarySearch(char*word,int n,char* result) //n为字典d中message的个数
{
strcpy(result,"eh");//如果没有查到就放回eh,这是题目的要求
int low=0;
int high=n-1;
int mid=0;
while(low<=high)
{
mid=(low+high)/2;
int flag=strcmp(d[mid].fo,word);
if(flag==0)
{
strcpy(result,d[mid].en);
return ;
}
if(flag>0)
{
high=mid-1;
}
else low =mid+1;
}
}
int main()
{
int n=0;
char s[40];
while(gets(s))
{
int len=strlen(s);
if(len==0)
break;
int i=0;
for(i=0; s[i]!=' ';i++ )
d[n].en[i]=s[i];
d[n].en[i]='\0';
int k=0;
for(int j=i+1; j<len ; j++)
{
d[n].fo[k]=s[j];
k++;
}
d[n].fo[k]='\0';
n++;
}
//对字典按照外文的字典顺序排序
QuickSort(n);
// cout<<"排序后的结果"<<endl;
//for(int i=0;i<n;i++)
//cout<<d[i].en<<" "<<d[i].fo<<endl;
char word[11];
while(cin>>word)// 输入要查询的外文单词放到数组words中
{
char result[11];
BinarySearch(word,n,result);
cout<<result<<endl;
}
return 0;
}
- 2 回答
- 0 关注
- 113 浏览
添加回答
举报