Smallest Difference POJ
来源:互联网 发布:知之深爱之切经典 编辑:程序博客网 时间:2024/06/04 19:22
题意:给几个数,用这些数字组成两个数,求这差值的绝对值最小为多少。
分析:暴力 (next_permutation)直接计算, 这时要注意剪纸不然会超时(两个数开头为零的情况剪掉)。
收获:对于暴力求解剪枝很重要。
#include <iostream>#include <cstdio>#include <math.h>#include <stdlib.h>#include <algorithm>#include <string.h>using namespace std;int digit[15];int n;int Min;int get_min(int k){ while(digit[0]==0) next_permutation(digit,digit+k); int mid=(k+1)/2; do { if(digit[mid]) { int sum1=digit[0],sum2=digit[mid]; for(int i=1; i<mid; i++) { sum1=sum1*10+digit[i]; } for(int i=mid+1; i<k ; i++ ) { sum2=sum2*10+digit[i]; } if(abs(sum1-sum2)<Min) { Min=abs(sum1-sum2); } }// cout << sum1 << " " << sum2 << " " << abs( sum1 - sum2 ) << endl ; } while(next_permutation(digit,digit+k)); return Min;}int main (){ cin >> n; getchar (); char c; while(n--) { Min=10000000; memset(digit,0,sizeof(digit)); int k=0; while((c=getchar())!='\n') { if(c!=' ') digit[k++]=c-'0'; } if(k==1) cout << digit[0]<<endl; else if(k==2) cout << abs(digit[0]-digit[1]) <<endl; else { cout << get_min(k) <<endl; } } return 0;}
0 0
- 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
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- Smallest Difference(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
- Smallest Difference POJ
- UVA10474 Where is the Marble?
- c++ string
- 并归排序
- 第32天(就业班) hibernate框架概述、ORM概念、hibernate第一个实例、api讲解、查询方式、主配置文件、映射配置、主键映射
- 洛谷 1144_最短路计数_spfa
- Smallest Difference POJ
- Operation System: Multi-thread 多线程 v.s. 多进程
- 洛谷 P1629 邮递员送信
- 基础练习 Sine之舞
- 【Stanford-ML-Discussion】二值分类器的多值分类问题理解
- Java内存管理的小技巧
- 时钟节拍
- HDU1233 还是畅通工程(最小生成树模板题,Prime,kruskal算法)
- 欢迎使用CSDN-markdown编辑器