时间限制:1秒 内存限制:128兆
3 3 010 909 012 6 4 5237 2753 7523 5723 5327 2537
3 2700
1 #include <bits/stdc++.h> 2 using namespace std; 3 char a[10][10]; 4 int main() 5 { 6 int n,k; 7 while(cin>>n>>k) 8 { 9 int b[10]; 10 for(int i=1;i<=n;i++) 11 cin>>a[i]; 12 for(int i=1;i<=k;i++) 13 b[i]=i; 14 int m=1; 15 for(int i=1;i<=k;i++) 16 m*=i;//直接求k!用m来装k全排列的可能性 17 int output=0x3f3f3f3f; 18 for(int i=1;i<=m;i++) 19 { 20 next_permutation(b+1,b+1+k);//全排列 21 int c[10]; 22 memset(c,0,sizeof(c)); 23 for(int i=1;i<=n;i++) 24 { 25 for(int j=1;j<=k;j++) 26 { 27 c[i]=c[i]*10+a[i][b[j]-1]-‘0‘;//数组c用来装数组a排列后的值 28 } 29 } 30 sort(c+1,c+1+n);//排序,将数组c中的值进行升序排列 31 output=min(output,c[n]-c[1]);//维护一个最大值一个最小值相减,求最小差值 32 } 33 cout<<output<<endl; 34 } 35 return 0; 36 }
原文:http://www.cnblogs.com/ECJTUACM-873284962/p/6390966.html