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 IIIIllll at 2019-05-29 14:09:13 on Problem 2718
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> l;

void readline()
{
    l.clear();
    char a[50];
    cin.getline(a, 50);
    for(int i=0; a[i]!='\0';i++){
        if(a[i]>='0'&&a[i]<='9'){
            l.push_back(a[i]-'0');
        }
    }
}

int solve()
{
    int res=0x3fffffff;
    int a, b;
    do{
        a=0; b=0;
        for(int i=0;i<l.size()/2;i++){
            a *= 10;
            a += l[i];
        }
        for(int i=l.size()/2;i<l.size();i++){
            b *= 10;
            b += l[i];
        }
        if((l[0]==0&&a!=0)||(l[l.size()/2]==0&&b!=0))continue;
        res = min(res, abs(a-b));
    }while(next_permutation(l.begin(), l.end()));
    return res;
}


int main()
{
    int n;
    cin>>n;
    getchar();
    for(int i=0;i<n;i++){
        readline();
        cout<<solve()<<endl;
    }
    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