Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

本题优化才能更节约时间??(付代码)

Posted by visual at 2006-05-07 02:38:46 on Problem 2470
该程序的内存使用是408K,运行时间是93ms,请教如何才能使时间更少?
#include<stdio.h>
#include<malloc.h>
int isAm(int *a,int n)
{
  int i; int flag =1;
  for(i=0; i<n;i++)
  {
    if(a[a[i]-1] != (i+1))   return 0;      
  }  
  return flag;     
}
int main()
{
    int n,i;
    int  *a;
   while( scanf("%d",&n) != EOF)
   {
    if(n == 0) break;      
    a = (int*)calloc(n,sizeof(int));
    for(i=0;i<n;i++) scanf("%d",&a[i]);
    if(isAm(a,n)) printf("ambiguous\n");
    else printf("not ambiguous\n");
    free(a);   
  }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator