题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小

思路:暴力比较差最小值

  1. stl中的next_permutation()函数进行排列  注意:这个函数必须从小到大才可以排序
  2. 把所有排序写出,从中间切,前面一个数,后面一个数,找到最小

    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 切数问题 穷竭搜索的更多相关文章

  1. 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. 《挑战程序设计竞赛》2.1 穷竭搜索 POJ2718 POJ3187 POJ3050 AOJ0525

    POJ2718 Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6509   Acce ...

  3. 1.初级篇——最基础的"穷竭搜索”

    A.Lake Counting(POJ 2386) 题意: 由于最近的降雨,农夫约翰田地的各个地方都有水汇聚,用N x M(1 <= N <= 100; 1 <= M <= 1 ...

  4. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  5. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  6. 穷竭搜索:POJ 3187 Backward Digit Sums

    题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...

  7. AOJ 0525 Osenbei【穷竭搜索】

    AOJ 0525 题意: 有一个烤饼器可以烤r行c列的煎饼,煎饼可以正面朝上(用1表示)也可以背面朝上(用0表示).一次可将同一行或同一列的煎饼全部翻转.现在需要把尽可能多的煎饼翻成正面朝上,问最多能 ...

  8. 挑战程序2.1.4 穷竭搜索>>深度优先搜索

      深度优先搜索DFS,从最开始状态出发,遍历一种状态到底,再回溯搜索第二种. 题目:POJ2386  思路:(⊙v⊙)嗯  和例题同理啊,从@开始,搜索到所有可以走到的地方,把那里改为一个值(@或者 ...

  9. 挑战程序2.1.5 穷竭搜索>>宽度优先搜索

    先对比一下DFS和BFS         深度优先搜索DFS                                   宽度优先搜索BFS 明显可以看出搜索顺序不同. DFS是搜索单条路径到 ...

随机推荐

  1. 破解百度翻译页面api参数加密

    我们的目标 https://fanyi.baidu.com/      找到获取翻译的请求 是这个 https://fanyi.baidu.com/v2transapi 查看一下post提交的表单,是 ...

  2. ES6:string.raw浅析

    当前正学习ES6 ,遇到string.raw费心思,现将试验后的结果整理如下: 网上得来的试验: 语法 String.raw`templateStr`; String.raw(obj, ...subs ...

  3. C#oracle还原imp实例

    C#来做oracle还原,调用oracle自带函数imp.exe时,需要注意的是:1.imp.exe 中fromuser 和touser两个关键字: fromuser与exp.exe中的owner对应 ...

  4. [转]Todd.log - a place to keep my thoughts on programming 分布式架构中的幂等性

    Todd.log - a place to keep my thoughts on programming 理解HTTP幂等性 基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式. ...

  5. linux 查看dd进度

    Linux下显示dd命令的进度: dd if=/dev/zero of=/tmp/zero.img bs=10M count=100000 想要查看上面的dd命令的执行进度,可以使用下面几种方法: 比 ...

  6. Android ORM对象关系映射之GreenDAO建立多表关联

    https://blog.csdn.net/u010687392/article/details/48496299 利用GreenDAO可以非常方便的建立多张表之间的关联 一对一关联 通常我们在操作数 ...

  7. 【虚拟机-网关】如何在使用应用程序网关和 Nginx 的环境下实现强制 HTTPS 跳转

    背景介绍 大家在使用 Nginx 部署网站时,实现 HTTP 到 HTTPS 的强制跳转是非常容易的事情,一般可以使用rewrite 命令或者使用返回自定义 301 页面的方法对 HTTP 请求进行 ...

  8. 参考消息 Android 读报

    <参考消息>是新华通讯社主办,参考消息报社编辑出版的日报,创刊于1931年,历史长达80年.<参考消息>每天及时选载世界各国(地区)通讯社.报刊及因特网上的最新消息.评论的精华 ...

  9. 事务回滚 DEMO

    因为有些事物回滚  查询的时候 可能查出来空值 我们肯定不愿意把空值添加数据库里面 一般基本的是这么写 if (object_id('add_T_Disclose_DiscloseList', 'P' ...

  10. AE开发关于OnMapReplaced方法的使用原理

    The OnMapReplaced event is triggered whenever the IMapControl2::Map is replaced by another map, such ...