课后自主练习(高精度)1076. 最小不重复数 super《编程思维与实践》个人学习笔记

题目

在这里插入图片描述

思路 暴力破解

字符串转换成数组,每次个位数+1
遍历一遍判断是否重复,然而超时了
进位越界问题在前面有提及这里不再赘述

代码

#include<iostream>
#include<cstring>
using namespace std;

struct BIG
{
    string str;
    int a[5000];
    int len;
    
    void init()
    {
        memset(a,0,sizeof(a));
        len = str.length();
        for(int i = 0, j = len - 1; i < len; i++, j--)
            a[i] = str[j] - '0'; 
    }
    void out()
    {
        for(int i = len - 1; i >= 0; i--)
            cout << a[i];
    }



    void carry()
    {
        for(int i = 0; i < len - 1; i++)
            if(a[i] >= 10)
            {
                a[i] -= 10;
                a[i + 1]++;
            }
        
        if(a[len - 1] >= 10)
        {   
            a[len - 1] -= 10;
            a[len]++;
            len++;
        }
    }
    int jud()
    {
        for(int i = len - 1; i > 0; i--)
        {
            if(a[i] == a[i - 1])
                return i;
        }

        return 0;
    }
    void func()
    {
        a[0]++;
        carry();
        while(jud())
        {
            a[0]++;
            carry();
        }
    }
};

int main()
{
    int t;
    cin >> t;
    for(int i = 0; i < t; i++)
    {
        BIG s;
        cin >> s.str;
        s.init();
        cout << "case #" << i << ":"<<endl;
        s.func();
        s.out();
        cout << endl;
    }

    return 0;
}

思路 优化

一开始还是得个位数+1,然后进位。
接着让字符串从前往后遍历,如果在中间发现了重复的内容,就让比较的两位中的后面的一位+1(别忘记进位)保证每次进的数据是前半部分不重复且最小的数。
再让后续的部分全部变成0,再重新从头遍历循环直到不重复为止

代码

#include<iostream>
#include<cstring>
using namespace std;

struct BIG
{
    string str;
    int a[5000];
    int len;
    
    void init()
    {
        memset(a,0,sizeof(a));
        len = str.length();
        for(int i = 0, j = len - 1; i < len; i++, j--)
            a[i] = str[j] - '0'; 
    }
    void out()
    {
        for(int i = len - 1; i >= 0; i--)
            cout << a[i];
    }

    

    void carry()
    {
        for(int i = 0; i < len - 1; i++)
            if(a[i] >= 10)
            {
                a[i] -= 10;
                a[i + 1]++;
            }
        
        if(a[len - 1] >= 10)
        {   
            a[len - 1] -= 10;
            a[len]++;
            len++;
        }
    }

    int jud()
    {
        for(int i = len - 1; i > 0; i--)
        {
            if(a[i] == a[i - 1])
                return i - 1;
        }

        return -1;
    }
    void func()
    {
        a[0]++;
        carry();
        while(int loc = jud() + 1)
        {
            a[loc - 1]++;
            for(int i = loc-2; i >= 0; i--)
                a[i] = 0;
            carry();
        }
    }
};

int main()
{
    int t;
    cin >> t;
    for(int i = 0; i < t; i++)
    {
        BIG s;
        cin >> s.str;
        s.init();
        cout << "case #" << i << ":"<<endl;
        s.func();
        s.out();
        cout << endl;
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值