POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

                                                      Smallest Difference

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

1
0 1 2 4 6 7

Sample Output

28

Source

Rocky Mountain 2005

题意:给一升序集合 集合中元素范围为1~9  从中寻找两个不相交子集(每个数只能用一次) 求这两个子集组成两个整数的差最小值

这题要注意一个情况,除了组成的数只有0,否则都不能以0为开头,如01是不存在的, 它并不等于1。 如果输入012,那么答案是 10-2 = 8,并不是2-1(01) = 1。

分析:这道题数据量很小 可以用next_permutation 来枚举所有的情况 由于只是要找差最小 所以枚举完只需要把枚举的情况从前到后组成两个位数相等(偶数情况) 或者 位数差1(奇数情况)的数 然后相减取绝对值(避免先后问题),然后取这些情况的最小值即可。

 

next_permutation函数将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为减序为止,使用方法是next_permutation(begin(),end())。

代码中以一个地方写的很妙 可以排除所有首元素为0的情况:

while(a[0] ==0)
next_permutation(a,a+cnt);//第一个数不能为0

因为next_permutation函数不会重复生成已经生成过的序列

这样就先排除了所有首数字为0的情况

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
char aa[20];
int a[20];
int cnt, mid, ans;
int main()
{
    int t;
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        cnt = 0;
        int t;
        while((t = getchar()) != '\n')
        {
            if(t != ' ')
            {
                aa[cnt] = t;
                cnt++;
            }
        }

        int mid = cnt/2;
        ans = 1e9;
        if(cnt == 2)
        {
            printf("%d\n",aa[1]-aa[0]);
            continue;
        }
        for(int i = 0; i < cnt; i++)
        {
            a[i] = aa[i] - '0';
        }
        while(a[0] ==0)
            next_permutation(a,a+cnt);//第一个数不能为0
        do
        {
            if(a[mid])
            {
                int t1=0,t2=0;
                for(int i=0;i<mid;++i)
                    t1=t1*10+a[i];
                for(int i=mid;i<cnt;++i)
                    t2=t2*10+a[i];
                ans=min(ans,abs(t1-t2));
                }

        }while(next_permutation(a,a+cnt));
        printf("%d\n",ans);


    }
}
Next_permutation

 

前面说过 这题数据量很小 可以用枚举来实现 复杂度为O(n²) 但如果数据量增大 枚举就行不通了 这时候可以使用贪心的思想来做这道题。

设t1为大数 t2为小数

如果是奇数的情况 那么因为t1的位数比t2多一位 那么t1最高位挑选最小的元素 t2最高位挑选次小的元素 然后t1依次挑选尽可能小的元素 t2依次挑选尽可能大的元素 组成的数t1-t2就是答案

如果是偶数的情况 那么就需要枚举一下每个相邻的位    t1的最高位挑选相邻位置中较大的元素 t2最高位挑选相邻位置较大的元素 然后t1依次挑选尽可能小的元素 t2挑选尽可能大的元素

  

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
char aa[20];
int a[20];
int cnt, mid;
int main()
{
    //freopen("1.txt","r",stdin);
    //freopen("2.txt","w",stdout);
    int t;
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        cnt = 0;
        int t;
        while((t = getchar()) != '\n')
        {
            if(t != ' ')
            {
                aa[cnt] = t;
                cnt++;
            }
        }

        int mid = cnt/2;

//        printf("%d\n",cnt);
        if(cnt == 2)
        {
            printf("%d\n",aa[1]-aa[0]);
            continue;
        }
        for(int i = 0; i < cnt; i++)
        {
            a[i] = aa[i] - '0';
        }
        if(cnt %2 == 0)//偶数情况
        {
            int t1,t2;//t1小数 t2大数
            bool vis[20];
            memset(vis,0,sizeof(vis));
            int ans = 1e9;

            for(int i = 0; i < cnt-1;i++)
            {
                if(a[i] == 0) continue;
                t1 = a[i];
                t2 = a[i+1];
//                printf("1  %d %d\n",t2, t1);
                vis[i]=1;vis[i+1] = 1;
                int ti = 0,pos = cnt - 1;
                while(ti < cnt/2-1 && pos >= 0)
                {
                    if(!vis[pos])
                    {
                        t1 = t1*10 + a[pos];
                        vis[pos] = 1;
                        ti++;
                    }
                    pos--;
                }
                ti = 0,pos = 0;
                while(ti < cnt/2-1 && pos < cnt)
                {
                    if(!vis[pos])
                    {
                        t2 = t2*10 + a[pos];
                        ti++;
                        vis[pos] = 1;
                    }
                    pos++;
                }
                memset(vis,0,sizeof(vis));
//                printf("2  %d %d\n",t2, t1);
                ans = min(ans,t2- t1);
            }

             printf("%d\n",ans);
        }
        else//奇数情况
        {
            int t1 , t2;//t1为大数 t2 为小数
            if(a[0] != 0)
            {
                t1 = a[0];
                t2 = a[cnt-1];
                for(int i = 0; i <= mid;i++)
                {
                    if(i == 0) continue;
                    t1 = t1*10 + a[i];
                }
                for(int i = cnt-2; i > mid; i--)
                {
                    t2 = t2*10 + a[i];
                }
                printf("%d\n",t1-t2);
            }
            else
            {
                t1 = a[1];
                t2 = a[cnt-1];
                for(int i = 0; i <= mid;i++)
                {
                    if(i == 1) continue;
                    t1 = t1*10 + a[i];
                }
                for(int i = cnt-2; i > mid; i--)
                {
                    t2 = t2*10 + a[i];
                }
                printf("%d\n",t1-t2);
            }


        }

    }
}
贪心

 

转载于:https://www.cnblogs.com/Jadon97/p/6763634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本火锅店点餐系统采用Java语言和Vue技术,框架采用SSM,搭配Mysql数据库,运行在Idea里,采用小程序模式。本火锅店点餐系统提供管理员、用户两种角色的服务。总的功能包括菜品的查询、菜品的购买、餐桌预定和订单管理。本系统可以帮助管理员更新菜品信息和管理订单信息,帮助用户实现在线的点餐方式,并可以实现餐桌预定。本系统采用成熟技术开发可以完成点餐管理的相关工作。 本系统的功能围绕用户、管理员两种权限设计。根据不同权限的不同需求设计出更符合用户要求的功能。本系统中管理员主要负责审核管理用户,发布分享新的菜品,审核用户的订餐信息和餐桌预定信息等,用户可以对需要的菜品进行购买、预定餐桌等。用户可以管理个人资料、查询菜品、在线点餐和预定餐桌、管理订单等,用户的个人资料是由管理员添加用户资料时产生,用户的订单内容由用户在购买菜品时产生,用户预定信息由用户在预定餐桌操作时产生。 本系统的功能设计为管理员、用户两部分。管理员为菜品管理、菜品分类管理、用户管理、订单管理等,用户的功能为查询菜品,在线点餐、预定餐桌、管理个人信息等。 管理员负责用户信息的删除和管理,用户的姓名和手机号都可以由管理员在此功能里看到。管理员可以对菜品的信息进行管理、审核。本功能可以实现菜品的定时更新和审核管理。本功能包括查询餐桌,也可以发布新的餐桌信息。管理员可以查询已预定的餐桌,并进行审核。管理员可以管理公告和系统的轮播图,可以安排活动。管理员可以对个人的资料进行修改和管理,管理员还可以在本功能里修改密码。管理员可以查询用户的订单,并完成菜品的安排。 当用户登录进系统后可以修改自己的资料,可以使自己信息的保持正确性。还可以修改密码。用户可以浏览所有的菜品,可以查看详细的菜品内容,也可以进行菜品的点餐。在本功能里用户可以进行点餐。用户可以浏览没有预定出去的餐桌,选择合适的餐桌可以进行预定。用户可以管理购物车里的菜品。用户可以管理自己的订单,在订单管理界面里也可以进行查询操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值