POj-2718 Smallest Difference--全排列next_permutation()的应用文章标题
来源:互联网 发布:java log4j stdout 编辑:程序博客网 时间:2024/05/22 15:30
next_permutation(s.begin(),s.end())列出下一个较大的组合
prev_permutation(s.begin(),s.end())列出上一个较小的组合
#include<stdio.h>#include<algorithm>#include<cmath>using namespace std;int main() { char the_char_string[100]; int the_digit_string[13]; int the_num; scanf("%d", &the_num); getchar(); while (the_num--) { gets_s(the_char_string); int len = 0; int min_abs = 100000000; for (int i = 0; i<strlen(the_char_string); i++) { if ('0'<=the_char_string[i]&&the_char_string[i]<='9') { the_digit_string[len++] = the_char_string[i] - '0'; } } sort(the_digit_string, the_digit_string + len); do { if (the_digit_string[0] == 0) { continue; } else if (the_digit_string[len / 2] == 0&&len>2) { //处理前面的数字多一个的情况,此时后面的数字串不能以0开头 int num1 = 0, num2 = 0; for (int i = 0; i < len / 2 + 1; i++) num1 = num1 * 10 + the_digit_string[i]; for (int i = len / 2 + 1; i < len; i++) num2 = num2 * 10 + the_digit_string[i]; min_abs = min(min_abs, abs(num1 - num2)); } else { //处理后面与前面一样或后面的比前面多一个的情况 int num1 = 0, num2 = 0; for (int i = 0; i < len / 2; i++) num1 = num1 * 10 + the_digit_string[i]; for (int i = len / 2; i < len; i++) num2 = num2 * 10 + the_digit_string[i]; min_abs = min(min_abs, abs(num1 - num2)); } } while (next_permutation(the_digit_string, the_digit_string + len)); printf("%d\n", min_abs); } return 0;}
O(∩_∩)O谢谢阅读!!!
阅读全文
0 0
- POj-2718 Smallest Difference--全排列next_permutation()的应用文章标题
- POJ 2718 Smallest Difference(暴力,全排列,next_permutation)
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- POJ 2718 Smallest Difference(DFS+全排列函数)
- poj 2718 Smallest Difference 【dfs(全排列变形题)】
- POJ 2718 Smallest Difference(dfs 全排列)
- Smallest Difference(poj 2718)二进制枚举+全排列
- poj 2718 Smallest Difference 穷竭搜索之全排列
- POJ 2718 Smallest Difference(贪心 or next_permutation()暴力枚举)
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
- POJ 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ-2718-Smallest Difference
- poj 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ 2718-Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- JAVA中AES对称加密和解密
- PHP中htmlentities和htmlspecialchars的区别
- el表达式保留两位小数、使用jstl fmt formatNumber标签保留两位小数
- JavaScript-prototype属性
- 【Android】关于在Service中通过接口回调更新Activity的UI
- POj-2718 Smallest Difference--全排列next_permutation()的应用文章标题
- 1.12.ARM的异常处理方式简单介绍
- 一:基本数据类型
- 如何用php截取文章的一段话以及第一个图片
- 微笑---面对所遭遇的一切
- iOS 百度地图~申请key及下载流程
- 树状数组
- MySQL基本函数介绍
- 从汇编视角看函数调用