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 Andrew613 at 2020-02-05 17:51:16 on Problem 2718
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
    int num=0;
    int  numbers[20];
    long long res=100000;
    scanf("%d\n",&num);
    for (int i=0; i<num; i++) {
        memset(numbers, 0, sizeof(numbers));
        char c=0;
        long long len=0;
        while(1){
        c=getchar();
        if (c>='0'&&c<='9') {
            numbers[len]=c-'0';
            len++;
        }
            if (c=='\n') {
                break;
            }
        }
        int j=0;
        while (next_permutation(numbers,numbers+len)) {
              int n1=0,n2=0;
              if (len%2==0) {
                  if ((numbers[0]=='0'||numbers[len/2]=='0')&&len>1) {
                              continue;
                          }
                      for (j=0; j<len/2; j++) {
                          n1=10*n1+numbers[j]-'0';
                      }
                      for (; j<len; j++) {
                       n2=10*n2+numbers[j]-'0';
                      }
                  }else{
                      if ((numbers[0]=='0'||numbers[len/2]=='0')&&len>1) {
                          continue;
                      }
                      for (j=0; j<len/2; j++) {
                          n1=10*n1+numbers[j]-'0';
                       }
                       
                       for (; j<len; j++) {
                           n2=10*n2+numbers[j]-'0';
                       }
                          
                  }
            long long t=0;
            t=abs(n1-n2);
            res=res>t?t:res;
            
        }
        printf("%lld\n",res);
    }
    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