不超过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,或者通过创建一个每次都会执行相同操作的函数来删除它们。
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
结论
通过这篇文章,我们更接近理解这些问题背后的原因,并理解了这些概念,这些概念将帮助我们在之前提到的重大实际问题中使用这些基本概念。就像在我们的代码中,我们分别解决每个问题,然后像制作美丽的手工品一样将代码缝合在一起,同样,我们将使用这个概念,尝试逐个解决问题。我们通常会从朴素的方法开始,但通过敏锐的眼光和努力,我们会找到更高效的方法。谁知道在阅读完这篇文章后,你会找到更好、更高效的方法,并进一步简化解决方案。所以,让我们坚持我们的信念和对思维和编码的信任,同时告别。
以上是不超过N且不包含S中任何数字的最大数字的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

待机是一种锁定屏幕模式,当iPhone插入充电器并以水平(或横向)方向定位时激活。它由三个不同的屏幕组成,其中一个是全屏时间显示。继续阅读以了解如何更改时钟的样式。StandBy的第三个屏幕显示各种主题的时间和日期,您可以垂直滑动。某些主题还会显示其他信息,例如温度或下一个闹钟。如果您按住任何时钟,则可以在不同的主题之间切换,包括数字、模拟、世界、太阳能和浮动。Float以可自定义的颜色以大气泡数字显示时间,Solar具有更多标准字体,具有不同颜色的太阳耀斑设计,而World则通过突出显示世界地

生成随机数或字母数字字符串的能力在许多情况下都会派上用场。您可以使用它在游戏中的不同位置生成敌人或食物。您还可以使用它向用户建议随机密码或创建文件名来保存文件。我写了一篇关于如何在PHP中生成随机字母数字字符串的教程。我在这篇文章的开头说,几乎没有事件是真正随机的,同样的情况也适用于随机数或字符串生成。在本教程中,我将向您展示如何在JavaScript中生成伪随机字母数字字符串。在JavaScript中生成随机数让我们从生成随机数开始。我想到的第一个方法是Math.random(),它返回一个浮

在任何语言中编写程序时,将数字表示为输出是一项有趣且重要的任务。对于整数类型(short、long或medium类型的数据),很容易将数字表示为输出。对于浮点数(float或double类型),有时我们需要将其四舍五入到特定的小数位数。例如,如果我们想将52.24568表示为三位小数,需要进行一些预处理。在本文中,我们将介绍几种技术,通过四舍五入将浮点数表示为特定的小数位数。在不同的方法中,使用类似C的格式化字符串、使用精度参数以及使用数学库中的round()函数是很重要的。让我们逐个来看。带有

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

在PHP编程语言中,is_numeric()函数是一种非常常用的函数,用于判断一个变量或值是否为数字。在实际编程中,经常需要对用户输入的数值进行验证,判断其是否为数字类型,这时就可以使用is_numeric()函数进行判断。一、is_numeric()函数简介is_numeric()函数是一个用于检测变量或值是否为数字的函数。如果变量或值为数字,则返回tru

Java中的数字重要的是要理解数字类不是一个有形的类,而是一个抽象的类。在它内部,我们有一组定义其功能的包装类。这些包装类包括Integer、Byte、Double、Short、Float和Long。您可能会注意到,这些与我们之前讨论的基本数据类型相同,但它们表示为具有大写名称的单独类,以符合类命名约定。根据特定函数或程序范围的要求,编译器自动将原始数据类型转换为对象,反之亦然,并且数字类是java.lang包的一部分。此过程称为自动装箱和拆箱。通过掌握数字类及其对应的包装类的抽象性质,我们可以

在本文中,我们将讨论查找1到n(给定)之间的数字的问题,这些数字不能被2到10之间的任何数字整除。让我们通过一些例子来理解这一点-Input:num=14Output:3Explanation:Therearethreenumbers,1,11,and13,whicharenotdivisible.Input:num=21Output:5Explanation:Therearefivenumbers1,11,13,17,and19,whicharenotdivisible.求解的方法简单方法如果

在数学中,5的整除规则规定,如果数字以0或5结尾,则它可以被5整除。还有另一种方法来确定5的整除规则,如果余数为0,则返回该数字能被5整除。mod(%)运算符通常在编程中用于整除。让我们举一个例子。给定的数字是525,该数字以5结尾,并且可以被5整除。给定的数字是7050,该数字以0结尾,并且可以被5整除。给定的数字是678,该数字不以0和5结尾,并且不能被5整除。在本文中,我们将解决该数字是否能被5整除的问题。算法以下步骤是我们将使用java.util.*包来获取原始数据类型的用户输入。从主类
