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 isblem 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 method
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 ApproachExample
#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); }
77777
Example
#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; }
149999
通过这篇文章,我们更接近理解这些问题背后的原因,并理解了这些概念,这些概念将帮助我们在之前提到的重大实际问题中使用这些基本概念。就像在我们的代码中,我们分别解决每个问题,然后像制作美丽的手工品一样将代码缝合在一起,同样,我们将使用这个概念,尝试逐个解决问题。我们通常会从朴素的方法开始,但通过敏锐的眼光和努力,我们会找到更高效的方法。谁知道在阅读完这篇文章后,你会找到更好、更高效的方法,并进一步简化解决方案。所以,让我们坚持我们的信念和对思维和编码的信任,同时告别。
以上是不超過N且不包含S中任何數字的最大數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!