Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一个暴力枚举的答案,轻喷!#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator