Smallest Difference
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10387   Accepted: 2836

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

1
0 1 2 4 6 7

Sample Output

28

Source

题解:给一串0到9的数,选择几个组成num1,剩下的组成num2,问最小的差值,当有两个数字时不能0开始;暴力,先选数字然后全排列
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int ans;
int vis[];
int p[] = {,,,,,,,,,};
int get(int* a){
int i = ;
while(){
char c = getchar();
if(c == ' ')continue;
if(c == '\n')return i;
a[i ++] = c - '';
vis[c - ''] = ;
} } int cal(int *a, int n){
int x = ;
for(int i = ; i < n; i++){
// printf("%d ", a[i]);
x = x * + a[i];
}//printf("\n计算的结果是:\n", x);
return x;
} void distribute(int* a, int an, int* l, int ln, int* r, int rn, int i){ if(i == an){
// puts("l的元素是:");
// for(int j = 0; j < ln; j++){
// printf("%d ", l[j]);
// }puts("");
//
// puts("r的元素是:");
// for(int j = 0; j < rn; j++){
// printf("%d ", r[j]);
// }puts(""); if(ln == || rn == ){
return;
}
if(abs(rn - ln) > ){
return;
}
do{
do{
if(ln > && l[] == ){
continue;
}
if(rn > && r[] == ){
continue;
}
int x = cal(l, ln);
int y = cal(r, rn);
// printf("x = %d\ny = %d\n", x, y);
if(abs(x - y) <= ans){
ans = abs(x - y);
}
}while(next_permutation(r, r + rn));
}while(next_permutation(l, l + ln)); return;
} l[ln] = a[i];
distribute(a, an, l, ln + , r, rn, i + );
r[rn] = a[i];
distribute(a, an, l, ln, r, rn + , i + ); } int main(){
int T;
scanf("%d", &T);
getchar();
int a[], l[], r[];
while(T--){
memset(vis, , sizeof(vis));
int n = get(a);
ans = 0x3f3f3f3f;
// if(n == 10){
// puts("247");
// continue;
// }else if(n == 9){
// int m[10] = {2469,10469,469,369,359,358,359,369,469,1469};
// for(int i = 0; i < 10; i++){
// if(!vis[i]){
// printf("%d\n", m[i]);
// }
// }
// continue;
// }
distribute(a, n, l, , r, , );
printf("%d\n", ans);
}
return ;
}
 

Smallest Difference(暴力全排列)的更多相关文章

  1. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  2. POJ2718Smallest Difference(暴力全排列)

    传送门 题目大意:升序输入十进制数 没有重复 分成两个非空集合 每个集合组成一个数(不能有前导零) 求两个数差的最小值. 题解:全排列...我数组从1开始怎么一直WA...还有这个输入值得学习. 代码 ...

  3. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  4. 【POJ - 2718】Smallest Difference(搜索 )

    -->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...

  5. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

  6. LintCode "The Smallest Difference"

    Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...

  7. Smallest Difference(POJ 2718)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6740   Accepted: 18 ...

  8. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  9. The Smallest Difference

    Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...

随机推荐

  1. MacBook如何用Parallels Desktop安装windows7/8

    虽然MacBook真的很好用,不过在天朝的国情下,有很多软件还是仅支持IE和windows系统下才有.所以有必要为自己的MacBook装一个windows版本的系统,之前试过用Boot Camp来建立 ...

  2. Android训练课程(Android Training) - NFC基础

    NFC 基础 本文档介绍了在Android上的基本的NFC任务.它说明了如何发送和接收的NDEF消息(NDEF messages)的形式的表单里包含的NFC数据(NFC data),并介绍Androi ...

  3. HTML5数据推送SSE原理及应用开发

    JavaScript表达行为,CSS表达外观,注意HTML既表达结构(逻辑结构),又表达内容(数据本身)通常需要更新数据时,并不需要更新结构,正是这种不改变组织结构仅改变数据的诉求,推动了数据拉取和数 ...

  4. Vue路由获取路由参数

    vue路由设置路由参数有2种方式: 1.通过query配置: <router-link :to="{ name:'login',query:{id:1} }">登录&l ...

  5. C++头文件的工作原理

    一.C++编译模式通常,在一个C++程序中,只包含两类文件——.cpp文件和.h文件.其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码:而.h文件则被称作C++头文件,里面放的也是C+ ...

  6. USB学习笔记连载(十一):CY7C68013A的启动方式-EEPROM

       上述的应用笔记中有介绍FX2LP的启动选项,主要包括I2C启动和USB启动. 说白了I2C启动需要使用外部的EEPROM,USB启动,只是使用上位机控制软件将配置程序FX2LP中,不用EEPRO ...

  7. c语言for循环等语句详解

    循环结构有: . goto语句和if语句构成循环 .while语句 .do-while语句 .for语句 goto语句 goto语句是一种无条件转移语句, 与Basic中的goto语句相似.goto语 ...

  8. (笔记)boa服务器make错误

    编译一个linux下的c系统,包含词法和语法分析模块,Linux上用bison和flex.yacc是一个文法分析器的生成器,bison即是yacc的GNU版本.Lex和YACC是用于构造词法分析机和语 ...

  9. 第三百二十四节,web爬虫,scrapy模块介绍与使用

    第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...

  10. e781. 设置JList中的选择模式

    // The default mode is MULTIPLE_INTERVAL_SELECTION String[] items = {"A", "B", & ...