第五届“传智杯”全国大学生计算机大赛 初赛B题 B-莲子的机械动力学

先上题目:

题目描述

题目背景的问题可以转化为如下描述:

给定两个长度分别为 n,m 的整数 a,b,计算它们的和。

但是要注意的是,这里的 a,b 采用了某种特殊的进制表示法。最终的结果也会采用该种表示法。具体而言,从低位往高位数起,第 i位采用的是 i+1 进制。换言之,相较于十进制下每一位的「逢 10进 1」,该种进制下第 i 位是「逢 i+1 进 1」。

下图所示,左边是十进制的竖式加法;右边是这种特殊进制的竖式加法。图中的红色加号表示上一位发生了进位。

输入格式

  • 第一行有两个整数 n,m,分别表示 a和 b的位数。
  • 第二行有 n 个整数,中间用空格隔开,从高到低位描述 a 的每个数码。
  • 第三行有 m 个整数,中间用空格隔开,从高到低位描述 b 的每个数码。

输出格式

  • 输出有若干个整数,从高到低位输出 a+b 在这种特殊表示法下的结果。

输入输出样例

输入

5 4
3 3 2 1 1
3 2 2 1

输出

4 2 1 1 0

输入 

10 1
10 9 8 7 6 5 4 3 2 1
0

输出 

10 9 8 7 6 5 4 3 2 1

说明/提示

对于全部数据,保证 1≤n,m≤2×10^5,从低位往高位数起有ai​∈[0,i],bi​∈[0,i]。请使用 Java 或 Python 语言作答的选手注意输入输出时的效率。

 

 个人思路:

 第一眼看见这个题目,脑子里想出来的就是高精度加法的模板。


但这个题很有意思,逢i+1进1,所以我们还需要在进位上做些文章。

2022.11.29个人优化后的代码:

 

#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main()
{
    int n,m;
    scanf("%d %d", &n, &m);
    deque<int> a, b,c;
    for (int i = 0; i < n; i++)
    {
        int t;
        scanf("%d", &t);
        a.push_front(t);
    }
    for (int i = 0; i < m; i++)
    {
        int t;
        scanf("%d", &t);
        b.push_front(t);
    }
    
    int t = 0; int count = 2;
    for (int i = 0; i <n||i<m; i++)
    {
            if(i<n)t += a[i];
            if(i<m)t += b[i];
            c.push_back(t % count);
            t /= count;
            count++;
    }
    while (t)
    {
        c.push_back(t % count);
        t /= count;
        count++;
    }
    int len = c.size();
    for (int i = len-1; i >=0; i--)
        printf("%d ", c[i]);
    return 0;

}

2022.11.26个人的原代码:  

 

#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main()
{
    int n,m;
    scanf("%d %d", &n, &m);
    deque<int> a, b,c;
    for (int i = 0; i < n; i++)
    {
        int t;
        scanf("%d", &t);
        a.push_front(t);
    }
    for (int i = 0; i < m; i++)
    {
        int t;
        scanf("%d", &t);
        b.push_front(t);
    }
    if (n > m)
    {
        int t = 0; int count = 2;
        for (int i = 0; i <n; i++)
        {
            t += a[i];
            if (i<m)
            {
                t += b[i];
            }
            c.push_back(t % count);
            t /= count;
            count++;
        }
        while (t)
        {
            c.push_back(t % count);
            t /= count;
            count++;
        }
    }
    else
    {
        int t = 0; int count = 2;
        for (int i = 0; i < m; i++)
        {
            t += b[i];
            if (i < n)
            {
                t += a[i];
            }
            c.push_back(t % count);
            t /= count;
            count++;
        }
        while (t)
        {
            c.push_back(t % count);
            t /= count;
            count++;
        }
    }
    int len = c.size();
    for (int i = len-1; i >=0; i--)
        printf("%d ", c[i]);
    return 0;

}

代码分析:

我们来分析一下改进后的代码:

一.数据的输入

int n,m;
    scanf("%d %d", &n, &m);
    deque<int> a, b,c;
    for (int i = 0; i < n; i++)
    {
        int t;
        scanf("%d", &t);
        a.push_front(t);
    }
    for (int i = 0; i < m; i++)
    {
        int t;
        scanf("%d", &t);
        b.push_front(t);
    }

输入

5 4
3 3 2 1 1
3 2 2 1

1.考虑到数据范围,我选择用容器来存储。

2.由于题目将n和m的每一位数都分开,用字符串string进行第一次存储过于麻烦,所以我选择直接用一个变量t进行第一次存储,再将t头插到deque容器。

3.为什么用deque容器?他能头插,我也考虑过用vector尾插,但判断条件的范围把握不好,所以选择deque进行头插。

二:进位相加:

int t = 0; int count = 2;
//t用来存储进位,count当做进制。
    for (int i = 0; i <n||i<m; i++)
    {
//当满足i<n或i<m时,循环仍能继续,这样保证两组数的每一位都能加上
            if(i<n)t += a[i];
            if(i<m)t += b[i];
            c.push_back(t % count);
//我们来假设一种情况,t=7,count=3
//将相加和取余,7%3=1,则将1留下,尾插到c容器中。
            t /= count;
//  7/2=3,将3留在t中,下一次进位时当做进位数
            count++;
//进制++,下一次就逢4进1。
    }
    while (t)
    {
        c.push_back(t % count);
        t /= count;
        count++;
//加到最后,若t还有剩余,就按照进制继续进位,直到t=0时结束
    }

输入

5 4
3 3 2 1 1
3 2 2 1

这是t没有多出的情况

来看看t多出的情况

 

 三:输出数据:

int len = c.size();
    for (int i = len-1; i >=0; i--)
        printf("%d ", c[i]);

由于我是用尾插,将数据存入c容器中,所以输出要倒着来,从容器的末尾开始输出。

 总结:

 个人能力水平有限,比赛中只做出来AB两题,文中若有错误,还请指出,如有疑问,欢迎留言。(比赛网站卡到让人血怒,交半天交不上去)

2022/11/29 12:18 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值