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

我穷举d-c,然后用O(n)的算法可以算出d-c==a+b?但程序出现一个好搞笑的问题,哪位大哥帮我看看

Posted by zerocool_08 at 2005-10-12 22:04:31 on Problem 2549
#include "stdio.h"
#include "stdlib.h"
int cmp(const void *p1,const void *p2)
{
    return *(int *)p1-*(int *)p2;
}    
int main(void)
{
    int   n,i,j,k,m,flag,a[1010],s,t=0,b[10]={0};
    while(scanf("%d",&n)==1&&n)
    {
        for(i=0;i<n;i++)
           scanf("%d",&a[i]);
    
      flag=0;
      qsort(a,n,sizeof(int),cmp);
      for(i=n-1;!flag&&i>=0;i--)
         for(j=n-1;!flag&&j!=i&&j>=0;j--)   //从n-1递减就WA,把j从0递增的话就AC了,T T
           for(m=0,k=n-1;m<k;){
               if(m==i||m==j)
                   m++;
               if(k==i||k==j)
                   k--;
               if(m<k)
               {
                   if(a[i]-a[j]==a[m]+a[k])
                   {  flag=1,s=a[i];b[t++]=s;break;}
                   else
                   if(a[i]-a[j]<a[m]+a[k])
                        k--;
                   else
                      m++;
               }    
           }  
       if(flag)
         printf("%d\n",s);
       else
         printf("no solution\n");
    }
    printf("\n\n%d   %d   %d  %d  %d\n",b[0],b[1],b[2],b[3],b[4]);
    scanf("%d",&n);
}      

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