首页 > 其他分享 >POJ2718(穷举,贪心)

POJ2718(穷举,贪心)

时间:2023-08-23 10:05:21浏览次数:36  
标签:map include int POJ2718 pos printf 穷举 size 贪心


参考地址

一开始连题意都没搞懂就开始直接做,too young。应该静下来用5分钟分析,by the way, may be the typical usage of brute-force is “void” function instead of function with return value.

//#define LOCAL

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <algorithm>
#define SIZE 10

using namespace std;

int map[SIZE];

int brute(int size){
    if(size == 1)
        return map[0];
    else if(size == 2)
        return abs(map[0] - map[1]);

    int ans = -1;
    int mid = size / 2;

    do{
        if(map[0] != 0 && map[mid] != 0){
            int x = 0;
            int y = 0;

            for(int i = 0; i < mid; i++)
                x = x * 10 + map[i];
            for(int i = mid; i < size; i++)
                y = y * 10 + map[i];

            if(ans == -1 || ans > abs(x - y))
                ans = abs(x - y);

            /*for(int i = 0; i < size; i++)
                printf("%d ", map[i]);
            printf("\n");
            printf("x:%d y:%d\n", x, y);*/
        }
    }while(next_permutation(map, map + size));

    return ans;
}

int main(){
#ifdef LOCAL
    freopen("data.in", "r", stdin);
#endif
    int n;
    scanf("%d", &n);
    getchar();

    while(n--){
        int pos = 0;
        char c;
        while(scanf("%c", &c), c != '\n'){
            if(c != ' ')
                map[pos++] = c - '0';
        }

        /*for(int j = 0; j < pos; j++)
            printf("%d ", map[j]);
        printf("\n");*/
        //printf("%d", pos);

        printf("%d\n", brute(pos));
    }

    return 0;   
}


标签:map,include,int,POJ2718,pos,printf,穷举,size,贪心
From: https://blog.51cto.com/u_8999467/7199308

相关文章

  • hdu 1003 最大最长上升子序列 贪心
    要想找到符合条件的序列,我们应该有以下条件 一个数重头开始遍历相加,如果这个数大于0的话,继续加后面的数,如果小于0的话,重后面的数开始重新遍历;这个过程中保证了大数一定会出现,所以应该找出大数;sum大于0的话,与后面的数相加有可能是最大数;如果小于0,则,重新开始会比以前的数更大;一下是......
  • Prim算法是一种用于解决最小生成树问题的贪心算法。它通过逐步选择边来构建最小生成树
    importjava.util.*;classPrimAlgorithm{privatestaticfinalintINF=Integer.MAX_VALUE;publicvoidprimMST(int[][]graph){intvertices=graph.length;int[]parent=newint[vertices];//用于存储最小生成树的父节点int......
  • Kruskal算法是一种用于寻找图的最小生成树的贪心算法。它通过按照边的权重递增的顺序
    Kruskal算法可以通过生活中的例子来解释。我们可以将城市之间的道路网络看作是一个图,每个城市是一个顶点,道路是连接城市的边,而道路的长度可以看作是边的权重。假设我们想要修建一条连接所有城市的最小成本道路网络。首先,我们需要找到连接城市的所有道路,并按照道路的长度进行排......
  • 贪心,构造学习笔记
    贪心构造不会黄题绿题懵逼横批:依托答辩\(\text{CF1764C}\)题目描述有一些点,每一个点有一个点权\(a_i\)。你可以在任意点之间连边,最终的图需要满足不存在\(a,b,c\)满足\(a_a\leqslanta_b\leqslanta_c\)并且\(ab,bc\)之间有连边。思路点拨我们连出来的图一定可以......
  • 贪心算法--活动选择问题
    博客地址:https://www.cnblogs.com/zylyehuo/#-*-coding:utf-8-*-defactivity_selection(a):res=[a[0]]foriinrange(1,len(a)):ifa[i][0]>=res[-1][1]:#当前活动的开始时间小于等于最后一个入选活动的结束时间#不冲......
  • 贪心算法--拼接最大数字问题
    博客地址:https://www.cnblogs.com/zylyehuo/#-*-coding:utf-8-*-fromfunctoolsimportcmp_to_keydefxy_cmp(x,y):ifx+y<y+x:return1#表示x>yelifx+y>y+x:return-1#表示x<yelse:re......
  • 贪心算法--背包问题--分数背包
    博客地址:https://www.cnblogs.com/zylyehuo/#-*-coding:utf-8-*-stuffs=[(60,10),(100,20),(120,30)]#每个商品元组表示(价格,重量)stuffs.sort(key=lambdax:x[0]/x[1],reverse=True)deffractional_backpack(goods,w):m=[0for_inr......
  • 贪心算法--找零问题
    博客地址:https://www.cnblogs.com/zylyehuo/#-*-coding:utf-8-*-t=[100,50,20,5]defchange(t,n):m=[0for_inrange(len(t))]#m为各面额纸币的张数fori,moneyinenumerate(t):m[i]=n//moneyn=n%money#n......
  • 反悔贪心
    遍历时,贪心选择权重最高的值,同时将没有选择的信息保留,以便反悔替换1.子序列的最大优雅度2.3n块披萨......
  • 几道典型的贪心算法练习题-适合入门
    1、看电视题目描述暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?输入输入包含多组测试数据。每组输入的第一行是一个整数n(n<=100),表示小明喜欢的节目的总数。接下......