POJ2718——Smallest Difference(暴力瞎搞)
来源:互联网 发布:源mac地址全是0的报文 编辑:程序博客网 时间:2024/06/04 23:34
题目链接
按从小到大的顺序给你一些数字,不会重复,要求你把这些数字分成两类,每一类可以组成一个整数,要求两类整数绝对值之差最小。输出最小差值。笔者表示并不会优化~~~大致看了一下,最恶心情况下,也就是给了10个数,然后暴力需要约400万次,所以就直接暴力瞎搞了,用了一个牛逼哄哄的函数next_permutation(),并且注意只给了两个数的情况单独写一下,和暴力过程中的前导零判断一下,然后直接暴力一波飘过........(应该是数据水了。。。。)
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;int a[15]={0};int k;int Search(){ int temp=k/2; int ans=99999999; while(next_permutation(a,a+k)) { int x=0,y=0; int term=1; if((a[0]==0&&temp-1!=0)||(a[temp]==0&&k-1!=temp))continue; for(int i=temp-1;i>=0;i--) { x+=term*a[i]; term*=10; } term=1; for(int i=k-1;i>=temp;i--) { y+=term*a[i]; term*=10; } ans=min(ans,abs(x-y)); } return ans;}int main(){ //freopen("in.in","r",stdin); int t; scanf("%d",&t); char c=getchar(); while(t--) { k=0; memset(a,0,sizeof(a)); while((c=getchar())&&c!='\n') if(c!=' ')a[k++]=c-'0'; if(k==2) { printf("%d\n",abs(a[1]-a[0])); continue; } int ans=Search(); printf("%d\n",ans); }}
0 0
- POJ2718——Smallest Difference(暴力瞎搞)
- POJ2718——Smallest Difference
- POJ2718 Smallest Difference (暴力搜索+全排列)
- poj2718 Smallest Difference
- poj2718 Smallest Difference
- POJ2718---Smallest Difference
- [dfs]poj2718 Smallest Difference
- POJ2718-Smallest Difference
- Smallest Difference poj2718
- POJ2718 Smallest Difference
- POJ2718-Smallest Difference
- poj2718 Smallest Difference【贪心】
- POJ2718 Smallest Difference
- poj2718 Smallest Difference
- POJ2718 Smallest Difference 【贪心+枚举】
- POJ2718-Smallest Difference-穷竭搜索
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- 【POJ2718】Smallest Difference(枚举排列)
- 远程文件拷贝命令scp的用法
- 为什么1000 == 1000返回为False,而100 == 100会返回为True?
- Mac 下gcc环境安装
- C#设计模式- 观察者模式(3)
- C#上机练习2
- POJ2718——Smallest Difference(暴力瞎搞)
- Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo
- 在gdb中显示源码 -- gdbtui使用方法
- 安卓热修复之——大搜集
- C#设计模式- 观察者模式(4)
- 详解Unity Profiler内存分析问题
- UILabel设背景颜色的时候出现一条线条
- Python中使用Beautiful Soup库的超详细教程
- Android studio 运行出现Error running app: This version of Android Studio is incompatible with the Gradle