POJ 2718 Smallest Difference 全排列函数next_permutation再尝试

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

大致题意:
给定一些个位数(升序输入,至少2个,至多10个(0~9) ),使用这些个位数任意组合成两个较大的数字,求这两个数字差的绝对值的最小值。
注意: 每个个位数只能使用一次,除非其中的一个较大数字为0,否则不应该以0开头。

解题思路:
暴力搜索(穷竭搜索),两个数差的绝对值最小,那么这两个数的位数一定是尽可能得接近。next_permutation函数能够很方便地求出全排列。

		//常用形式:即先排序,再使用该函数  letter为字符数组
		sort(letter, letter + strlen(letter), cmp);
		do {
			cout << letter << endl;
		} while (next_permutation(letter, letter + strlen(letter), cmp));

AC代码:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stdio.h>//在poj上使用G++提交时且未包含该头文件时 无法编译getchar()
using namespace std;
#define INF 0x3fffffff
string num;
vector<int> a;
void readline() {
	//getchar();
	a.clear();
	getline(cin, num);
	for (int i = 0; i < num.size(); i++) {
		if (num[i] == ' ') { continue; }
		a.push_back(num[i] - '0');
	}
}

int main() {
	int n, ans;
	cin >> n;
	getchar();//吃掉回车
	while (n--) {
		ans = INF;
		readline();
		do {//两个数差的绝对值最小,那么这两个数的位数一定是尽可能得接近
			int ans1 = 0; int ans2 = 0;
			if (a.size() > 2 && (a[0] == 0 || a[a.size() / 2] == 0)) continue;
			for (int i = 0; i < a.size() / 2; i++) {
				ans1 = ans1 * 10 + a[i];
			}
			for (int i = a.size() / 2; i < a.size(); i++) {
				ans2 = ans2 * 10 + a[i];
			}
			ans = min(ans, abs(ans1 - ans2));
		} while (next_permutation(a.begin(), a.end()));
		cout << ans << endl;
	}
	//system("pause");
	return 0;
}

解题笔记:
1、在poj上使用G++提交时且未包含stdio.h头文件时 无法编译getchar()
2.、题目中需要先输入一个整数,然后回车,然后读取带有空格的整行,那么需要在读入整数后吃掉回车。
3、使用容器Vector真的好方便(╯▽╰)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值