标签:href 中间 strong put getchar getc char 一个 NPU
题目:http://poj.org/problem?id=2718
给一组十进制数,从中选出若干数字组成一个多位数,剩下的数字组成另一个数字,要求找到最小的选取组合,使得这两个数字的绝对值差最小。
样例输入:
1
0 1 2 4 6 7
样例输出:
28
思路:
代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10;
int num[maxn];
int array2num(int *start, int *end){
int val = 0;
for(; start < end; start++){
val = val * 10 + (*start);
}
return val;
}
int main(){
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int M; scanf("%d", &M);
char c; c = getchar();
while(M--){
int idx = 0;
int min_val = 0x3f3f3f3f;
int val1, val2;
while((c = getchar()) != '\n'){
if(c != ' ') num[idx++] = c-'0';
}
if(idx == 1) { printf("%d", num[0]); return 0;} //idx是数字个数
do{
int mid = idx / 2;
if(idx - mid > 1 && num[mid] == 0) continue; //去掉含有前导零的情况
else if(mid > 1 && num[0] == 0) continue;
else{
val1 = array2num(num, num+mid);
val2 = array2num(num+mid, num+idx);
}
min_val = min(abs(val1-val2), min_val);
}while(next_permutation(num, num+idx));
printf("%d\n", min_val);
}
}
标签:href 中间 strong put getchar getc char 一个 NPU
原文地址:https://www.cnblogs.com/patrolli/p/12205174.html