pku acm oj 1002 ~time limit exceeded~help!

来源:百度知道 编辑:UC知道 时间:2024/06/15 01:33:10
是power()用太多?
还是quicksort?
还是cout的问题??

如何解决啊??

#include <iostream>
#include <string>
using namespace std;

int power(int s,int p)
{
int j=s;
s=1;
for(int i=0;i<p;i++)
{
s*=j;
}
return s;
}

void exchange(int c[],int a,int b)
{
int temp;
temp=c[a];
c[a]=c[b];
c[b]=temp;
}

int partition (int c[],int p,int r)
{
int i,x,j;
x=c[r];
i=p-1;
for (j=p;j<=r-1;j++)
if (c[j]<=x){
i++;
exchange(c,i,j);
}
exchange(c,i+1,r);
return i+1;
}

void quicksort(int c[],int p,int r)
{
int q;
if (p<r){
q=partition(c,p,r);
quicksort(c,p,q-1);
quicksort(c,q+1,r);

你的程序写的太繁杂.. 很多东西都没有必要, 参考下我的程序吧(当然是ac的)

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define MAXN 10000003
int n, cn[MAXN], len;
char a[100];
const int mapp[30] = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, -1, 7, 7, 8, 8, 8, 9, 9, 9, -1};

int main()
{
int i, t, ans;
cin>>n;
while (n--)
{
scanf("%s", a);
len = strlen(a);
ans = 0;
for (i=0; i<len; i++)
{
if (a[i]>='0'&&a[i]<='9') t = a[i]-'0';
else if (a[i]>='A'&&a[i]<='Z') t = mapp[a[i]-'A'];
else t =-1;
if (t != -1) ans *= 10, ans += t;
}
cn[ans]++;
}
int sign = 0;
for (i=0; i<MAXN; i++)
{
if (cn[i] > 1)
{
printf("%03d-%04d %d\n", i/10000, i%10000, cn[i]);
sign = 1;