讲师中心 微信公众号
AI工具推荐 视频效率加速

不超过N且不包含S中任何数字的最大数字

老伟酱_2447

老伟酱_2447

发布时间:2023-09-05 17:17:03

|

1446人浏览过

|

来源于tutorialspoint

转载

不超过n且不包含s中任何数字的最大数字

The challenge of finding the largest number not exceeding a given number N and not containing any of the digits in a string S is a problem that involves string manipulation and number theory. The goal is to determine the greatest possible number that is less than or equal to N while also excluding all of the digits found in the string S.

例如,考虑一个情景,其中N等于1000,S等于"42"。在这种情况下,最大的不超过N且不包含S中任何数字的数是999。这是因为999是使用数字0、1、3、5、6、7、8和9组成的最大可能数,而不包括字符串S中的数字4和2。

Different approaches can be used to solve this problem, such as iterating through all numbers up to N and verifying if their digits are not present in S, or by utilizing more complex methods like dynamic programming or backtracking.

Algorithm

步骤 1 − 我们将在main()函数中声明两个名为‘N’和‘S’的字符串变量。

第二步 - 我们将把这两个变量作为参数传递给LargestNumberFinder()函数。

Step 3 − We will convert the string number N and S into integer implicitly to do mathematical operations such as comparision.

步骤 4 - 我们将手动删除存储在 N 中的数字中的前导 0,或者通过创建一个每次都会执行相同操作的函数来删除它们。

2026年数字艺术字分层PSD素材下载
2026年数字艺术字分层PSD素材下载

2026年数字艺术字分层PSD素材适用于2026数字艺术字设计 本作品提供2026年数字艺术字分层PSD素材的图片会员免费下载,格式为PSD,文件大小为13.2M; 请使用软件Photoshop进行编辑,作品中文字及图均可以通过软件修改和编辑;

下载

Step 5 − Then, we will start comparing the digits of the both the strings and finding out which is the largest number formed not more than ‘N’ that doesn’t contain any digit from string ‘S’.

Approach 1: - Naïve Approach

使用另一个字符串中的所有数字来查找给定字符串中最大的数字的基本方法如下。主函数声明变量并调用LargestNumberFinder函数。该函数以两个字符串作为输入,检查每个小于N的值,该值在字符串S中具有所有的数字。如果满足条件,则以字符串格式返回该值。attendance函数用于确定存储在'i'中的值是否是字符串S的一部分,同时将S转换为整数数据类型。输入字符串被转换为整数,并使用循环来评估条件。代码输出在给定字符串中具有所有数字的最大数值,该数值在另一个字符串中也存在。

Example

的翻译为:

示例

该代码是一个解决方案,它找到比N(输入字符串转换为整数)小的最大数字,该数字由字符串S中的数字组成。该代码利用两个函数,'attendance'和'LargestNumberFinder'来确定并返回最大数字。attendance函数以整数'i'和字符串's'作为输入,检查存储在'i'中的值是否是字符串's'的一部分,并将's'转换为整数数据类型。LargestNumberFinder函数以两个字符串'x'和's'作为输入,将'x'转换为整数,然后使用attendance函数检查所有小于N且所有数字都在's'中的值。主函数声明变量并调用LargestNumberFinder函数,该函数将最大数字作为字符串返回。

#include <iostream>
#include <string>
#include <vector>

// function to check whether value stored in ‘i’ is part of string S while also converting S into integer data type.
bool attendance(int i, std::string s) {
   while (i) {
      int first_digit = i % 10;
      i /= 10;
      int t = std::stoi(s);
      bool found = false;
      while (t) {
         int second_digit = t % 10;
         t /= 10;
         if (second_digit == first_digit) {
            found = true;
            break;
         }
      }
      if (!found)
         return false;
   }
   return true;
}

// function to input two strings and check for each value less than N with all digits present in S.
std::string LargestNumberFinder(std::string x, std::string s) {
   int N = std::stoi(x);
   for (int i = N; i >= 1; i--) {
      if (attendance(i, s)) {
         return std::to_string(i);
      }
   }
   return "-1";
}

// main function to declare the variables and call the function.
int main() {
   std::string N = "100709";
   std::string S = "70";
   std::cout << LargestNumberFinder(N, S);
}

Output

77777

方法2:高效方法

对于问题2的解决方案,即通过将给定数字字符串N的数字替换为给定字符串S的数字,得到最大可能的数字,这是一种高效的方法。该方法首先检查S中是否存在N的每个数字,并用S中不在N中的最大数字替换第一个在S中找到的数字。然后,其余的数字将被替换为S中不在N中的最大数字。然后去掉前导零,并将结果作为最大可能的数字返回。这种方法比之前的方法更高效,因为它不需要对字符串进行排序。

Example

的翻译为:

示例

The code solves a problem of finding the greatest number that can be formed from a given string "N" by replacing a digit with the highest digit not present in the string "S". The code utilizes an efficient method to solve the problem. The LargestNumberFinder function takes two string inputs, "num" and "s", and returns the largest possible number. The vector "vis_s" is utilized to store the values of string "s". The code first identifies the first digit of string "num" that is part of string "s". Then it swaps that digit with the highest digit not present in string "s". The code then finds the highest digit not found in string "s" and replaces the rest of the digits in string "num" with that digit. The leading zeros are removed from the final string, and if the string is empty, the function returns "0". The code outputs the result by calling the function with inputs "N" and "S".

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// function to check for all values of String N with String S and replacing the digit if found same with the largest possible digit not present in S.
string LargestNumberFinder(string num, string s) {
   vector<bool> vis_s(10, false);
   for (int i = 0; i < (int)s.size(); i++) {
      vis_s[int(s[i]) - 48] = true;
   }
   int n = num.size();
   int in = -1;
   for (int i = 0; i < n; i++) {
      if (vis_s[(int)num[i] - '0']) {
         in = i;
         break;
      }
   }
   if (in == -1) {
      return num;
   }
   for (char dig = num[in]; dig >= '0'; dig--) {
      if (vis_s[(int)dig - '0'] == 0) {
         num[in] = dig;
         break;
      }
   }
   char LargestDig = '0';
   for (char dig = '9'; dig >= '0'; dig--) {
      if (vis_s[dig - '0'] == false) {
         LargestDig = dig;
         break;
      }
   }
   for (int i = in + 1; i < n; i++) {
      num[i] = LargestDig;
   }
   int Count = 0;
   for (int i = 0; i < n; i++) {
      if (num[i] == '0')
         Count++;
      else
         break;
   }
   num.erase(0, Count);
   if ((int)num.size() == 0)
      return "0";
   return num;
}
int main() {
   string N = "161516";
   string S = "756";
   cout << LargestNumberFinder(N, S);
   return 0;
}

Output

149999

结论

通过这篇文章,我们更接近理解这些问题背后的原因,并理解了这些概念,这些概念将帮助我们在之前提到的重大实际问题中使用这些基本概念。就像在我们的代码中,我们分别解决每个问题,然后像制作美丽的手工品一样将代码缝合在一起,同样,我们将使用这个概念,尝试逐个解决问题。我们通常会从朴素的方法开始,但通过敏锐的眼光和努力,我们会找到更高效的方法。谁知道在阅读完这篇文章后,你会找到更好、更高效的方法,并进一步简化解决方案。所以,让我们坚持我们的信念和对思维和编码的信任,同时告别。

热门AI工具

更多
切问学术

切问学术是一款AI论文写作工具,复旦大学NLP团队推出的AI学术智能体。

UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

AionClaw
AionClaw Hot

AionClaw是一款面向办公、创作和编程任务的AI桌面智能体。

火山引擎

火山引擎是一款面向企业的云计算与AI服务平台。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

蛙蛙写作

一款AI论文写作工具,主要用于超级AI智能写作助手,适合需要提升相关任务效率的用户。

墨刀AI
墨刀AI Hot

一款AI图像与设计工具,主要用于产品经理的专属智能体,适合需要提升相关任务效率的用户。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

相关专题

更多
Sass和less的区别
Sass和less的区别

Sass和less的区别有语法差异、变量和混合器的定义方式、导入方式、运算符的支持、扩展性等。本专题为大家提供Sass和less相关的文章、下载、课程内容,供大家免费下载体验。

2147

2023.10.12

数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2351

2023.10.31

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

494

2025.10.31

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

402

2026.02.12

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

5119

2023.08.02

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

334

2023.09.25

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

1538

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

2204

2023.09.04

Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

0

2026.09.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn