目錄
的翻譯為:
Output
结论
首頁 後端開發 C++ 不超過N且不包含S中任何數字的最大數字

不超過N且不包含S中任何數字的最大數字

Sep 05, 2023 pm 05:17 PM
數位 最大 不超過

不超過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 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 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 highest digit not present in the string "S"。 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 diifies the first git 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 theplaces 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

iOS 17:如何在待機模式下變更iPhone時鐘樣式 iOS 17:如何在待機模式下變更iPhone時鐘樣式 Sep 10, 2023 pm 09:21 PM

待機是一種鎖定螢幕模式,當iPhone插入充電器並以水平(或橫向)方向定位時啟動。它由三個不同的螢幕組成,其中一個是全螢幕時間顯示。繼續閱讀以了解如何變更時鐘的樣式。 StandBy的第三個畫面顯示各種主題的時間和日期,您可以垂直滑動。某些主題也會顯示其他訊息,例如溫度或下一個鬧鐘。如果您按住任何時鐘,則可以在不同的主題之間切換,包括數位、類比、世界、太陽能和浮動。 Float以可自訂的顏色以大氣泡數字顯示時間,Solar具有更多標準字體,具有不同顏色的太陽耀斑設計,而World則透過突出顯示世界地

JavaScript中產生隨機數字和字串 JavaScript中產生隨機數字和字串 Sep 02, 2023 am 08:57 AM

產生隨機數或字母數字字串的能力在許多情況下都會派上用場。您可以使用它在遊戲中的不同位置生成敵人或食物。您也可以使用它向用戶建議隨機密碼或建立文件名來保存文件。我寫了一篇關於如何在PHP中產生隨機字母數字字串的教學。我在這篇文章的開頭說,幾乎沒有事件是真正隨機的,同樣的情況也適用於隨機數或字串生成。在本教程中,我將向您展示如何在JavaScript中產生偽隨機字母數字字串。在JavaScript中產生隨機數字讓我們從產生隨機數開始。我想到的第一個方法是Math.random(),它回傳一個浮

C++程式將一個數字四捨五入到n位小數 C++程式將一個數字四捨五入到n位小數 Sep 12, 2023 pm 05:13 PM

在任何語言中編寫程式時,將數字表示為輸出是一項有趣且重要的任務。對於整數類型(short、long或medium類型的資料),很容易將數字表示為輸出。對於浮點數(float或double類型),有時我們需要將其四捨五入到特定的小數位數。例如,如果我們想將52.24568表示為三位小數,需要進行一些預處理。在本文中,我們將介紹幾種技術,透過四捨五入將浮點數表示為特定的小數位數。在不同的方法中,使用類似C的格式化字串、使用精度參數以及使用數學函式庫中的round()函數是很重要的。讓我們逐一來看。帶有

使用C++編寫程式碼,找到第N個非平方數 使用C++編寫程式碼,找到第N個非平方數 Aug 30, 2023 pm 10:41 PM

我們都知道不是任何數字的平方的數字,如2、3、5、7、8等。非平方數有N個,不可能知道每個數字。因此,在本文中,我們將解釋有關無平方數或非平方數的所有內容,以及在C++中尋找第N個非平方數的方法。第N個非平方數如果一個數是整數的平方,則該數稱為完全平方數。完全平方數的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一個數不是任何整數的平方,則該數稱為非平方數。例如,前15個非平方數是-2,3,5,6,

在PHP中使用is_numeric()函數檢查是否為數字 在PHP中使用is_numeric()函數檢查是否為數字 Jun 27, 2023 pm 05:00 PM

在PHP程式語言中,is_numeric()函數是一種非常常用的函數,用來判斷一個變數或值是否為數字。在實際程式設計中,常需要對使用者輸入的數值進行驗證,判斷其是否為數字類型,這時就可以使用is_numeric()函數來判斷。一、is_numeric()函數簡介is_numeric()函數是用來偵測變數或值是否為數字的函數。如果變數或值為數字,則傳回tru

Java中的數字(帶有0前綴和字串) Java中的數字(帶有0前綴和字串) Aug 29, 2023 pm 01:45 PM

Java中的數字重要的是要理解數字類不是一個有形的類,而是一個抽象的類。在它內部,我們有一組定義其功能的包裝類別。這些包裝類別包括Integer、Byte、Double、Short、Float和Long。您可能會注意到,這些與我們之前討論的基本資料類型相同,但它們表示為具有大寫名稱的單獨類,以符合類命名約定。根據特定函數或程式範圍的要求,編譯器會自動將原始資料類型轉換為對象,反之亦然,且數字類別是java.lang套件的一部分。此過程稱為自動裝箱和拆箱。透過掌握數字類別及其對應的包裝類別的抽象性質,我們可以

找出在範圍內不可被任何數整除的數字,使用C++ 找出在範圍內不可被任何數整除的數字,使用C++ Sep 13, 2023 pm 09:21 PM

在本文中,我們將討論查找1到n(給定)之間的數字的問題,這些數字不能被2到10之間的任何數字整除。讓我們透過一些例子來理解這一點-Input:num=14Output:3Explanation:Therearethreenumbers,1,11,and13,whicharenotdivisible.Input:num=21Output:5Explanation:Therearefivenumbers1,11,13,17,and19,whicharen的解題方法簡單方法如果

Java程序,用來檢查一個數字是否可以被5整除 Java程序,用來檢查一個數字是否可以被5整除 Sep 13, 2023 pm 09:01 PM

在數學中,5的整除規則規定,如果數字以0或5結尾,則它可以被5整除。還有另一種方法來決定5的整除規則,如果餘數為0,則傳回該數字能被5整除。 mod(%)運算子通常在程式設計中用於整除。讓我們舉一個例子。給定的數字是525,數字以5結尾,可以被5整除。給定的數字是7050,數字以0結尾,並且可以被5整除。給定的數字是678,數字不以0和5結尾,不能被5整除。在本文中,我們將解決該數字是否能被5整除的問題。演算法以下步驟是我們將使用java.util.*套件來取得原始資料類型的使用者輸入。從主類

See all articles