【算法题】进制转换、末尾0的个数、餐馆问题


一共三个问题,写于第一二次课后。
2017/11/17


1、问题一:进制转换

1.1 问题描述

问题链接

给定一个十进制数M,以及需要转换的进制数N。将十进制数M转化为N进制数。

输入描述:

输入为一行,M(32位整数)、N(2 ≤ N ≤ 16),以空格隔开。

输出描述:

为每个测试实例输出转换后的数,每个输出占一行。如果N大于9,则对应的数字规则参考16进制(比如,10用A表示,等等)

示例1

输入
7 2

输出
111

1.2 解题思路

思路:十进制转换成其他进制x,无非就是数字n循环除以x,逆序取余就是x进制的答案。当然如果是负数,提前加符号,如果是x超过10,则需要ABCDEF等字母来表示。然后转换成string输出即可。

1.3 代码

语言:C++ 运行时间: 4 ms 占用内存:384K 状态:答案正确

#include<iostream>
#include <vector>
#include <math.h>
#include <string>
using namespace std;

#if 1
//思路一
class Trange
{
public:
    string getnum(int n)
    {
        switch (n)
        {
        case 10:return "A";
        case 11:return "B";
        case 12:return "C";
        case 13:return "D";
        case 14:return "E";
        case 15:return "F";
        default:
            return "";
        }
    }
    string convert(int m, int n)
    {      
        vector<int>nums;
        string temp = "";
        if (m < 0)
        {
            m = 0 - m;
            temp = "-";
        }
        while (m > 0)
        {
            nums.push_back(m%n);
            m = m / n;
        }
        for (int i = nums.size() - 1; i >= 0; i--)
        {
            if (nums[i] < 10)
                temp = temp + to_string(nums[i]);
            else
                temp = temp + getnum(nums[i]);
        }
        return temp;
    }
};
//思路二
class Trange1
{
public:
    string convert(int m, int n)
    {
        string s = "012346789ABCDEF";
        string temp = "";
        if (m < 0)
        {
            m = 0 - m;
            temp = "-";
        }
        while (m > 0)
        {
            temp += s[m%n];
            m = m / n;
        }
        return temp;
    }
};
int main()
{
    int m, n;
    cin >> m >> n;
    Trange s;
    cout << s.convert(m, n) << endl;
    return 0;
}
#else
#endif

2、问题二:末尾0的个数

2.1 问题描述

问题链接

输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2。

输入描述:

输入为一行,n(1 ≤ n ≤ 1000)

输出描述:

输出一个整数,即题目所求

示例1

输入
10

输出
2

2.2 解题思路

对于一个阶乘,其末尾0是如何获得的,是通过2*5=10得到的,所以只要知道这个阶乘中有多少个2,有多少个5的即可。

而对于一个阶乘,其中因数2的个数一定多于5的个数,所以只要看5的个数即可。

因数中有多少个5,阶乘末尾就会有多少个0。

所以提供两种思路,一种是,对于每一个5的倍数x,计算x最大是5的多少幂次方,即5*5*…*5<=x中有多少个5。然后将每个x中5的个数累加起来即为总阶乘中的个数。

另一种是,5的个数等于,对于每一个数n,其有多少个n/5,n/(5*5),n/(5*5*5)…..

2.3 代码

语言:C++ 运行时间: 4 ms 占用内存:380K 状态:答案正确

#include <iostream>
using namespace std;

#if 1
//思路一
class zero
{
public:
    int zeronum(int n)
    {
        int count = 0;
        for (int i = 5; i <= n;i+=5)
        {
            int j = i;
            while (j % 5 == 0)
            {
                count++;
                j = j / 5;
            }          
        }
        return count;
    }
};
//思路二
class zero1
{
public:
    int zeronum(int n)
    {
        int count = 0;
        while (n)
        {
            count += n / 5;
            n = n / 5;
        }
        return count;
    }
};
int main()
{
    int n;
    cin >> n;
    zero1 s;
    cout << s.zeronum(n) << endl;
    //system("pause");
    return 0;
}
#else
#endif

3、问题三:餐馆

3.1 问题描述

问题链接

某餐馆有n张桌子,每张桌子有一个参数:a 可容纳的最大人数; 有m批客人,每批客人有两个参数:b人数,c预计消费金额。 在不允许拼桌的情况下,请实现一个算法选择其中一部分客人,使得总预计消费金额最大 。

输入描述:

输入包括m+2行。 第一行两个整数n(1 <= n <= 50000),m(1 <= m <= 50000) 第二行为n个参数a,即每个桌子可容纳的最大人数,以空格分隔,范围均在32位int范围内。 接下来m行,每行两个参数b,c。分别表示第i批客人的人数和预计消费金额,以空格分隔,范围均在32位int范围内。

输出描述:

输出一个整数,表示最大的总预计消费金额

示例1

输入
3 5 2 4 2 1 3 3 5 3 7 5 9 1 10

输出
20

3.2 解题思路

利用贪心的思想。

首先将m批客人进行排序,排序规则按照,先消费金额降序排序,再消费人数升序排列。

然后将n个桌子进行排序,排序规则按照桌子容量升序排列。

然后遍历排序后每一个批客人,找到第一个合适的桌子,即桌子容量大于等于消费人数,盈利金额累加该批客人的消费金额,并且将这个桌子从桌子数组中erase掉。

遍历结束,返回盈利金额。

3.3 代码

语言:C++ 运行时间: 491 ms 占用内存:2028K 状态:答案正确

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

#if 1
struct node
{
    int x;
    int y;
};
bool mycomp(node a, node b)
{
    if (a.y == b.y)
        return a.x - b.x < 0;
    return a.y - b.y > 0;
}
class resturant
{
public:
    long long  maxprofit(vector<int>n_num, vector<node>m_num)
    {
        long long profit = 0;
        sort(m_num.begin(), m_num.end(), &mycomp);
        sort(n_num.begin(), n_num.end());
        for (int i = 0; i < m_num.size();i++)
        {
            vector<int>::iterator it = n_num.begin();
            while (it != n_num.end())
            {
                if (*it>=m_num[i].x)
                    break;
                it++;
            }
            if (it != n_num.end())
            {
                n_num.erase(it);
                profit = profit + m_num[i].y;
            }
        }
        return profit;
    }

};
int main()
{
    int n, m;//n是桌子数,m是批次
    cin >> n >> m;
    vector<int>n_num;
    int x;
    for (int i = 0; i < n;i++)
    {
        cin >> x;
        n_num.push_back(x);
    }
    vector<node>m_num;
    for (int i = 0; i < m;i++)
    {
        int x1, x2;
        cin >> x1 >> x2;
        node temp;
        temp.x = x1;
        temp.y = x2;
        m_num.push_back(temp);
    }
    resturant r;
    cout << r.maxprofit(n_num, m_num) << endl;
    //system("pause");
    return 0;
}
#else
#endif
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值