poj2718 枚举排列
来源:互联网 发布:知乎国外智慧城市案例 编辑:程序博客网 时间:2024/06/06 12:21
Smallest Difference
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13000 Accepted: 3512
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.
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
10 1 2 4 6 7
Sample Output
28
题意:给出n组数据,根据每组数据排列组成两个数,使得两数之差最小。
分析:next_permutation枚举排列,但是输入是个麻烦事。
1.对于这种输入多行不定长整数的,使用gets或者getline输入一整行数据,然后把字符转化为整数存储起来;
2.在整数数组的基础上,利用next_permutation进行排列的枚举;
3.对于每种枚举,生成两个长度为n/2和n-n/2的数,求差值。
ps:
gets是输入到字符数组,getline是输入到字符串,相对来说后者更安全;
使用getline时,如果之前使用了scanf,cin等,要用getchar清除掉缓冲区中的回车符。
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <iostream>#include <string>using namespace std;#define INF 0x3f3f3f3fint n;string line;int num[11];int main(){cin >> n;getchar();while (n--){int i = 0;int ans = INF;getline(cin,line);for (int j = 0; j < (int)line.size(); j += 1)if(line[j]>='0' && line[j]<='9') num[i++] = line.at(j)-'0';if(i == 2) {printf("%d\n",num[1]-num[0]);continue;}do{if(num[0] == 0 || num[i/2] == 0) continue;int asum = 0,bsum = 0;for (int j = 0; j < i/2; j += 1) asum = asum*10+num[j];for (int j = i/2; j < i; j += 1) bsum = bsum*10+num[j];ans = min(ans,(int)abs(bsum-asum));}while (next_permutation(num,num+i));printf("%d\n",ans);}return 0;}
阅读全文
0 0
- poj2718 枚举排列
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- 【POJ2718】Smallest Difference(枚举排列)
- POJ2718(枚举 + 贪心)
- POJ2718 Smallest Difference 【贪心+枚举】
- POJ2718 Smallest Difference (暴力搜索+全排列)
- POJ2718
- poj2718
- poj2718
- poj2718
- 枚举排列
- 枚举排列
- 枚举排列
- 枚举排列
- 枚举排列
- 枚举排列
- 枚举排列
- 枚举排列
- 线性表应用_约瑟夫环问题
- MT4函数_读取CSV格式文件存入到二维数组
- 5.4银行不同期限的年息利率
- Storm之——搭建Storm集群
- 松柏先生:参透五百年不遇的《求贤令》,何愁品牌不立!
- poj2718 枚举排列
- 【JZOJ5439】【NOIP2017提高A组集训10.31】Calculate
- 20171101 hello world
- 5-2 派生类的构造函数
- JZOJ5442【NOIP2017提高A组冲刺11.1】荒诞 三进制状压+欧拉序
- 临时
- 数据结构实验之二叉树七:叶子问题(层序遍历输出叶子)
- 数组和对象间的比较
- Chrome保存mht网页文件的方法