POJ 2718
来源:互联网 发布:剑网3插件数据 编辑:程序博客网 时间:2024/05/01 02:43
题意:
对于一个数列,我们将数字分成两堆,求可能的情况下,两堆数字组成的数的差的最小值,排除前导0
题解:
将给出的数列进行字典序排列,然后将每个部分的转化成数字做差比较,因为字典序,所以枚举了所有数字
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>#define pr(x) cout<<#x<<" "<<x;#define pl(x) cout<<#x<<" "<<x<<endl;#include<math.h>#include<algorithm>#include<set>#include<map>#include<bitset>#include<string>using namespace std;int calnum(int str[],int eindex){ int a= 0; for(int i = 0 ;i<eindex;i++){ a = str[i]+a*10; } return a;}int main(){ int t; cin>>t; cin.ignore(); while(t--){ string s; getline(cin,s); s.erase(remove(s.begin(),s.end(),' '),s.end()); int length = s.size(); int nums[length]; for(int i = 0;i<length;i++){ nums[i] = s[i]-'0'; } int numa[length]; int numb[length]; int bendindex; int ans=0x7f7f7f7f; if(length%2==0){ bendindex = length/2; } else{ bendindex = length/2+1; } do{ for(int i = 0;i<length/2;i++){ numa[i] = nums[i]; } for(int i = length/2,j = 0;i<length;i++,j++){ numb[j] = nums[i]; } if((numa[0]==0&&length/2>1)|| (numb[0]==0&&bendindex>1) ){continue;} else{ int a = calnum(numa,length/2); int b = calnum(numb,bendindex); ans = min(ans,abs(a-b)); } }while(next_permutation(nums,nums+length)); cout<<ans<<endl; }}
0 0
- poj 2718
- poj 2718
- POJ 2718
- poj 2718
- POJ 2718
- poj 2718
- poj 2718
- 【POJ】2718
- 【POJ】2718
- poj 2718
- poj 2718
- poj 2718 搜索
- POJ 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ-2718-Smallest Difference
- poj 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ 2718-Smallest Difference
- FatMouse' Trade
- springmvc注解
- IOS懒加载不调用问题解决方案
- ssh自动认证
- spring jdbcTemplate连接数据库
- POJ 2718
- Chrome 控制台console的用法
- 接木棍(优先队列)
- Hadoop升级方案(二):从Hadoop 1.0升级到2.0(1)
- RS485接口标准、硬件电路
- hadoop2升级的那点事情(详解)
- RS422接口标准、硬件电路
- hadoop2.7.0升级到2.7.1,版本升级
- Leetcode Sudoku Solver9