题目
Smallest Difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19791 Accepted: 5395
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

Rocky Mountain 2005

PS.**************************************************************************************************
看了别人的题解之后,发现了一个很好用的输入方式:cin>>skipws>>c>>noskipws>>s ,一般c和s都为char类型的变量,skipws的作用是输入时忽略空格回车符,而noskipws的作用显然是不跳过空格和回车,所以有时候输入一段字符串时,可以用此方法代替getline() (其实是因为我不会/(ㄒoㄒ)/~~),下面贴一下使用方法,input:0 1 2 3 4 ouput:1234。

#include <iostream>
#include <cstdio>using namespace std;char a[100];int main() {int k=0;char c, s;while(cin>>skipws>>c>>noskipws>>s) {a[k++]=c;if(s=='\n') break;}for(int i=0;i<k;i++) printf("%c",a[i]);return 0;
}

思路
想法十分简单,用nex_permutation() 对数组进行全排列,每次取一半进行分界,计算两边的差的绝对值。但有一点需要注意下,就是当g[0]==0或者g[size/2]==0时,要判断下左右边各自的总和,如果其中有值不为0,则违反了题目的要求。下面贴AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>using namespace std;#define maxx 10
#define inf 0x7fffffffvector<int> g;void read() {g.clear();char a[50];cin.getline(a,50);for(int i=0;a[i]!='\0';i++) {if(a[i]>='0' && a[i]<='9') {g.push_back(a[i]-'0');}}
}void solve() {int ans=inf;int a,b;do {a=0, b=0;for(int i=0;i<g.size()/2;i++) {a*=10;a+=g[i];}for(int j=g.size()/2;j<g.size();j++) {b*=10;b+=g[j];}if((g[0]==0 && a!=0) || (g[g.size()/2]==0 && b!=0)) continue;ans=min(ans,abs(a-b));} while(next_permutation(g.begin(),g.end()));printf("%d\n",ans);
}int main() {int n;scanf("%d",&n);getchar();for(int i=0;i<n;i++) {read();solve();}return 0;
}

POJ 2718 Smallest Difference相关推荐

  1. POJ 2718 Smallest Difference 贪心构造

    不用说,想让两个数的差最小,那这个两个数的位数要最接近. 不妨设a>b; 如果n是奇数 a就是最小的(n/2+1)位数,b就是最大的n/2位数 如果是偶数 枚举每一对相邻的数,大的作为a的第一位 ...

  2. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  3. POJ 2718 Smallest Difference

    分奇数和偶数两种情况进行讨论,奇数可贪心,偶数没想到贪心的方法,采用直接枚举的方法过的~~~ #include <cmath> #include <cstdio> #inclu ...

  4. LintCode 387: Smallest Difference

    LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...

  5. POJ 2718【permutation】

    POJ 2718 问题描述: 给一串数,求划分后一个子集以某种排列构成一个数,余下数以某种排列构成另一个数,求这两个数最小的差,注意0开头的处理. 超时问题:一开始是得到一个数列的组合之后再从中间进行 ...

  6. POJ 2718 贪心

    做了一上午的2718,差点爆炸 做法一: 用到了STL中的next_permutation产生所有的全排列 要使生成的两个数差最小,两个数的位数差肯定小于等于1,产生一个排列之后在中间分开就可以 什么 ...

  7. ACM Smallest Difference

    给定一些不同的十进制数字(distinct decimal digits),您可以通过选择这些数字的非空子集(non-empty subset)并以某种顺序编写它们,从而形成一个整数. 剩下的数字可以 ...

  8. poj2718 Smallest Difference

    思路: 暴力乱搞. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <sstream> 4 #in ...

  9. (全排列)Smallest Difference (poj2718)

    题目: Description - 题目描述 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来 ...

最新文章

  1. Java Socket 编程
  2. MobileNet教程(2):用TensorFlow搭建安卓手机上的图像分类App
  3. 最短路径Dijkstra讲解,工具包使用 python
  4. android的四种启动模式,(转)彻底弄懂Activity四大启动模式
  5. 使用 unsafe_使用Unsafe真的是关于速度或功能吗?
  6. Python: 使用装饰器“@”取得函数执行时间
  7. 没有样式重置_我不能没有的5个Vue.js库
  8. 5G商用正式启动:最全套餐资费详情都在这里了
  9. 大数据_Hbase-(概念补充_hbase中namespace的概念)---Hbase工作笔记0007
  10. 在Linux下轻松搭建自己的DNS服务器
  11. python 爬虫生成csv文件和图_Python简单爬虫导出CSV文件
  12. 用于机器学习的数据库--UCI数据库
  13. android模拟器录制脚本,什么安卓模拟器可实现操作录制?MuMu模拟器成为你的按键精灵_MuMu安卓模拟器/MuMu手游助手...
  14. ZeroMQ知识总结大全(二):Req-Rep模式详解
  15. nginx源码分析—reuseport的使用
  16. DataGear 制作Excel动态数据可视化图表
  17. 特征预处理:归一化/标准化/缺失值
  18. c语言中puts的作用,c语言puts函数用法是什么?
  19. AD16 绘制简单电路原理图的基本步骤(适合小白)
  20. 天翼云荣获2022年度“边缘计算先锋企业”“分布式云先锋企业”称号!

热门文章

  1. Python爬取清朝末年医书:《醉花窗医案》,看看病症情况
  2. 陌陌COO王力访谈实录:当股价在过山车时 我们在想什么?
  3. 【BZOJ-2503】相框 并查集 + 分类讨论
  4. 获取 Android 模拟器的 MAC
  5. 基于asp.net的物流配送信息网站(含车主和货主)
  6. matplotlib绘制等比横纵坐标
  7. 德意志酒店集团启用新品牌,欲树奢华酒店新标杆
  8. 自己用的框架写了一个PHP模版解析类
  9. 天气转冷,注意保暖哦!
  10. 身份证的正确使用方法——很重要的知识(转贴)