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

TLE!!!怎么做呢?帮我看看吧

Posted by xiaol at 2005-10-23 12:54:35 on Problem 1785
#include <stdio.h>
#include <string.h>
int a[50000];
char s[50000][8];
void output(int i,int j) {
    int max=-1,k,p;
    if(i==j) {
        printf("(%s/%d)",s[i],a[i]);
        return ;
    }    
    for(k=i;k<=j;k++) {
        if(a[k]>max) {
            max=a[k];
            p=k;
        }
    }
    if(p==i) {
        printf("(");
        printf("%s/%d",s[p],a[p]);
        output(i+1,j);
        printf(")");
        return ;
    } 
    if(p==j) {
        printf("(");
        output(i,j-1);
        printf("%s/%d",s[p],a[p]);
        printf(")");
        return ;
    }
    printf("(");
    output(i,p-1);
    printf("%s/%d",s[p],a[p]);
    output(p+1,j);
    printf(")");
    return ;
}                  
int main(){
    int n,i,j,k;
    char c;
    int tem,len;
    int max,p;
    while(1){
        scanf("%d",&n);
        if(n==0) break;
        max=-1;
        if(n==1) {
            scanf("%s",&c,&s[0]);
            printf("(%s)\n",s[0]);
            continue;
        }    
        for(i=0;i<n;i++) {
            scanf("%s",s[i]);
            len=strlen(s[i]);
            j=len-1;
            k=1;tem=0;
            while(s[i][j]>='0' && s[i][j]<='9'){
                tem=tem+(s[i][j]-'0')*k;
                k=k*10;
                j--;
            }
            a[i]=tem;
            s[i][j]='\0';    
            if(max<a[i]) {
                max=a[i];
                p=i;
            }      
        } 
        if(p==0) {
            printf("(");
            printf("%s/%d",s[p],a[p]);
            output(p+1,n-1);
            printf(")\n");
            continue;
        }
        if(p==n-1) {
            printf("(");   
            output(0,p-1); 
            printf("%s/%d",s[p],a[p]);
            printf(")\n");
            continue;
        }    
        printf("(");
        output(0,p-1);
        printf("%s/%d",s[p],a[p]);
        output(p+1,n-1);
        printf(")\n");
    }
    return 0;
}            
        

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