poj 2718 切数问题 穷竭搜索
题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小
思路:暴力比较差最小值
- stl中的next_permutation()函数进行排列 注意:这个函数必须从小到大才可以排序
- 把所有排序写出,从中间切,前面一个数,后面一个数,找到最小
if (a[mid])
{
int x = a[0]; int y = a[mid];
for (int i = 1; i < mid; i++)
x = x * 10 + a[i];
for (int i = mid + 1; i < n; i++)
y = y * 10 + a[i];
if (ans > abs(x - y))
ans = abs(x - y);
}
解决问题的代码:
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <cstdio>
using namespace std;
int n, a[];
void solve()
{
while (a[] == ) next_permutation(a, a + n);
int ans = ;
int mid = (n + ) / ;
do {
if (a[mid])
{
int x = a[]; int y = a[mid];
for (int i = ; i < mid; i++)
x = x * + a[i];
for (int i = mid + ; i < n; i++)
y = y * + a[i];
if (ans > abs(x - y))
ans = abs(x - y);
}
} while (next_permutation(a, a + n));
printf("%d\n", ans);
return;
}
int main()
{
int t;
char c;
scanf("%d", &t);
getchar();
while (t--)
{
n = ;
memset(a, , sizeof(a));
while ((c = getchar()) != '\n')
{
if (c != ' ')
a[n++] = c - '';
}
if (n == ) printf("%d\n", a[]);
if (n == ) printf("%d\n", abs(a[] - a[]));
else solve();
}
return ;
}
poj 2718 切数问题 穷竭搜索的更多相关文章
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...
- 《挑战程序设计竞赛》2.1 穷竭搜索 POJ2718 POJ3187 POJ3050 AOJ0525
POJ2718 Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6509 Acce ...
- 1.初级篇——最基础的"穷竭搜索”
A.Lake Counting(POJ 2386) 题意: 由于最近的降雨,农夫约翰田地的各个地方都有水汇聚,用N x M(1 <= N <= 100; 1 <= M <= 1 ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- poj 2718 Smallest Difference(穷竭搜索dfs)
Description Given a number of distinct , the integer may not start with the digit . For example, , , ...
- 穷竭搜索:POJ 3187 Backward Digit Sums
题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...
- AOJ 0525 Osenbei【穷竭搜索】
AOJ 0525 题意: 有一个烤饼器可以烤r行c列的煎饼,煎饼可以正面朝上(用1表示)也可以背面朝上(用0表示).一次可将同一行或同一列的煎饼全部翻转.现在需要把尽可能多的煎饼翻成正面朝上,问最多能 ...
- 挑战程序2.1.4 穷竭搜索>>深度优先搜索
深度优先搜索DFS,从最开始状态出发,遍历一种状态到底,再回溯搜索第二种. 题目:POJ2386 思路:(⊙v⊙)嗯 和例题同理啊,从@开始,搜索到所有可以走到的地方,把那里改为一个值(@或者 ...
- 挑战程序2.1.5 穷竭搜索>>宽度优先搜索
先对比一下DFS和BFS 深度优先搜索DFS 宽度优先搜索BFS 明显可以看出搜索顺序不同. DFS是搜索单条路径到 ...
随机推荐
- linux debian 时间设置中无法选择“自动设定时间和日期”
没有安装ntpdate 执行:apt-get install ntpdate ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s1a.time.ed ...
- Dubbo 使用rest协议发布http服务
演示用GitHub地址:https://github.com/suyin58/dubbo-rest-example 1 Dubbo_rest介绍 Dubbo自2.6.0版本后,合并了dub ...
- Css Hack 大全(IE6、IE7、IE8、IE9 css hack)
一.IE6 css hack: 1. *html Selector {} /* Selector 表示 css选择器 下同 */ 2. Selector { _property: value; } / ...
- Ionic 2 中的创建一个闪视卡片组件
闪视卡片是记忆信息的重要工具,它的使用可以追溯到19世纪.我们将要创建一个很酷的短暂动画来实现它.看起来像是这个样子的: 闪视卡片示例 Ionic 2 实例开发 新增章节将为你介绍如何在Ionic 2 ...
- SharePoint 2013 安装配置(1)
在这篇文章中,我将逐步介绍在Windows Server 2012 R2上安装SharePoint 2013. 在进一步详细介绍之前,让我们先了解SharePoint 2013安装的硬件和软件要求.您 ...
- 新手第一天学习 C#语言(进制互换)
说起来我们对一些陌生或者未知的东西有一些恐惧感,但是又有一些期待,虽然我不确定自己能不能学会这门语言,但是我会尽自己最大的努力学. 我们第一天学的的内容呢,对大多数的人都知道,计算机的语言是二进制,但 ...
- javaSe-常用的类之Calender
Calendar是java中常用的类,比data类使用更加方便,更能更加强大,好吧多的不用你说了,直接上代码 import java.util.Calendar;//需要引进的包 public cla ...
- MYSQL内置函数总结
时间转换为字符串: SELECT date_format(Date, '%Y-%m-%d %H:%i:%s' ) AS TimeFROMtable o convert函数转换为字符串的时候不存在类型为 ...
- 谷歌浏览器 加安全地址 快捷方式加参数 chrome
--unsafely-treat-insecure-origin-as-secure="http://192.168.43.17:8080"
- Java压缩字符串工具类
StringCompressUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.By ...