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

Re向中间搜索

Posted by 1210812305 at 2010-11-04 13:10:57 on Problem 3617
In Reply To:跪求各位!!!帮帮我,怎么设计算法?当两边相等的时候怎么办?我这猪头实在是想不出来 Posted by:suying at 2008-07-20 16:51:37
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[10009],b[10009],ch;
int n; 
int test(int i,int j)
{
    int i1,j1;
    for(i1=i+1,j1=j-1;i1<j1;i1++,j1--)
    {
        if(a[i1]>a[j1])
        return j;
        if(a[i1]<a[j1])
        return i;
    }
    return i;
}
void cometonew()
{
     int i,j,kk=0;
     for(i=0,j=strlen(a)-1;i<=j; )
     {
        if(a[i]<a[j])
        {
           b[kk++]=a[i]; 
           i++;
        }
        else if(a[i]>a[j])
        {
           b[kk++]=a[j];
           j--;
        }
        else
        {   int pp=test(i,j);
            b[kk++]=a[pp];
            if(pp==i)
            i++;
            else
            j--;
        }
     }
     b[kk]='\0';
}
int main(int argc, char *argv[])
{   
    int i,j;
    cin>>n;
    getchar();
    for(i=0;i<n;i++)
    scanf("%c%c",&a[i],&ch);
    cometonew();
    j=strlen(b);
    for(i=0;i<j;i++)
    {
       printf("%c",b[i]);
       if((i+1)%80==0||i==j-1)
       printf("\n"); 
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

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