C++ 通过删除十六进制表示中在给定字符串中出现的字符来修改数组

C++ 通过删除十六进制表示中在给定字符串中出现的字符来修改数组

我们有一个包含正整数的数组,需要通过从当前元素的十六进制表示中删除给定的“hex”字符串中的字符来修改数组的每个元素。

为了解决这个问题,我们可以将当前数字转换为十六进制数字。然后,我们可以从十六进制字符串中删除在“hex”和当前十六进制字符串中都存在的字符。修改完十六进制字符串后,我们可以将其转换回十进制。

问题陈述 - 我们有一个包含正整数的数组,数组的长度为N。此外,我们还有一个包含0到9的数字和“A”到“F”字符的“hex”字符串。我们需要将每个数组值转换为十六进制,从其中删除在“hex”字符串中存在的数字的十六进制表示,并将其转换回十进制数。

示例

输入 - array[] = {92, 90, 75, 129},hex = ‘1BC’

输出 - [5, 90, 4, 8]

解释

  • 92的十六进制表示为’5C’。由于’C’在’hex’字符串中存在,我们必须将其删除。因此,结果字符串将为’5’;将其转换为十进制,得到5。

  • ’90’的十六进制值为’5A’。由于’5’或’A’都不在’hex’字符串中,它将保持不变。

  • 75的十六进制表示为’4B’,将其从中删除’B’,字符串变为’4’。将’4’转换为十进制,得到4。

  • 129的十六进制值为’81’。从修改后的十六进制字符串中删除’1’后,变为’8’,其十进制表示为8。

    输入 - array[] = {54, 43, 56, 9999, 1234, 32},hex = “1BCFE234”

    输出 - [6, 0, 8, 112, 13, 0]

    解释

  • 实际数字 -> 十六进制 -> 修改后的十六进制 -> 最终十进制

  • 54 -> 36 -> 6 -> 6

  • 43 -> 2B -> 0 -> 0

  • 56 -> 38 -> 8 -> 8

  • 9999 -> 270F -> 70 -> 112

  • 1234 -> 4D2 -> D -> 13

  • 32 -> 20 -> 0 -> 0

方法

在这种方法中,我们首先将十进制值转换为十六进制值。然后,我们将修改十六进制值。接下来,我们将把修改后的十六进制值转换为十进制,并用结果值替换数组元素。此外,我们将使用不同的方法来删除十六进制字符串中的字符,将十进制转换为十六进制,以及将十六进制转换为十进制数。

步骤

  • 在changeArr()函数中开始遍历每个字符串字符。

  • 使用decimalToHexa()函数将数组值转换为十六进制值,并将结果存储在‘hexa’字符串变量中。

    • 在decimalToHexa()函数中,如果数值为0,则返回0。

    • 定义alpha[]数组并存储字符。同时,定义‘hexa’字符串。

    • 迭代直到数字大于零为止。

    • 如果num % 16 <10 为真,则将num % 16附加到‘hexa’字符串。否则,附加alpha[num % 16 – 10]到‘hexa’字符串,因为我们需要为11到16的值附加字符。

    • 将num除以16。

    • 迭代完成while循环后,使用reverse()方法反转‘hexa’字符串并返回其值。

  • 现在,使用removeChars()函数从‘hexa’字符串中删除与给定‘hex’字符串相同的字符。

    • 在removeChars()函数中,定义名为‘charSet’的set,并将‘hex’字符串的所有字符插入其中。

    • 现在遍历‘hexVal’字符串。如果‘hexVal’字符串的当前字符存在于charset中,继续迭代。否则,将当前字符附加到临时的‘res’字符串中。

    • 返回‘res’字符串。

  • 现在,使用decimalToHexa()函数将修改后的十六进制字符串转换为十进制。

    • 在decimalToHexa()函数中,定义包含从10到15的数字的‘mp’数组。

    • 反转十六进制字符串。

    • 遍历十六进制字符串。将字符值乘以16的pos次方,并将其加到‘res’值中。

    • - 将‘pos’值加1。

  • 用‘dec’值替换数组元素。

示例

#include <bits/stdc++.h>
using namespace std;
string decimalToHexa(int num){
   if (num == 0) {
      return "0";
   }
   // char array to store hexadecimal number
   char alpha[] = {'A', 'B', 'C', 'D', 'E', 'F'};
   string Hexa;
   // Traverse the number until it becomes zero
   while (num > 0) {
      // if the remainder is less than 10, store it as it is
      if (num % 16 < 10) {
          Hexa += to_string(num % 16);
      } else {
          // else store the character
          Hexa += alpha[num % 16 - 10];
      }
      // divide the number by 16
      num /= 16;
   }
   // reverse the string
   reverse(Hexa.begin(), Hexa.end());
   return Hexa;
}
int hexaToDecimal(string modifiedHex) {
   // stores hexadecimal to decimal
   char mp[] = {10, 11, 12, 13, 14, 15};
   // Stores result
   int res = 0;
   int pos = 0;
   // reverse the string
   reverse(modifiedHex.begin(), modifiedHex.end());
   // Traverse the string
   for (char ch : modifiedHex) {
      // If a digit, multiply it with 16^pos
      if (isdigit(ch)) {
          res += ((int)pow(16, pos)) * (ch - '0');
      }
      // If character, multiply it with 16^pos
      else {
          res += ((int)pow(16, pos)) * mp[ch - 'A'];
      }
      // Increment the position
      pos += 1;
   }
   // Return the answer
   return res;
}
string removeChars(string hexaVal, string hex) {
   // set to store the characters of the given string
   set<char> charSet;
   // insert the characters of the given string in the set
   for (char ch : hex) {
      charSet.insert(ch);
   }
   // string to store the final hexadecimal number
   string res = "";
   // traverse the hexadecimal number string
   for (char ch : hexaVal) {
      // if the character is present in the set, then continue
      if (charSet.find(ch) != charSet.end()) {
          continue;
      }
      // else add the character to the final string
      res += ch;
   }
   // return the final string
   return res;
}
// function to modify the array as per the given condition
void changeArr(int array[], int N, string hex) {
   // Traverse the array
   for (int i = 0; i < N; i++) {
      // covnert the number to hexadecimal
      string hexa = decimalToHexa(array[i]);
      // remove the characters from the hexadecimal number string which are present in the given string
      string modifiedHex = removeChars(hexa, hex);
      // change the hexadecimal number to decimal
      int decVal = hexaToDecimal(modifiedHex);
      // replace the number with the new decimal value
      array[i] = decVal;
   }
   cout << "The final array after converting a number to hexadecimal, removing characters which are present in the string, and converting back to decimal is " << endl;
   // Print the modified array
   for (int i = 0; i < N; i++) {
      cout << array[i] << " ";
   }
}
int main() {
   // Given array
   int array[] = {54, 43,56,9999,1234, 32};
   int N = sizeof(array) / sizeof(array[0]);
   // Given string
   string hex = "1BCFE234";
   changeArr(array, N, hex);
   return 0;
}

抱歉,我不能直接保留HTML格式进行翻译。我可以将以下英文翻译成中文: 抱歉,我不能解释,但我可以做翻译。

输出

The final array after converting a number to hexadecimal, removing characters which are present in the string, and converting back to decimal is 
6 0 8 112 13 0

时间复杂度 – O(N*S),其中 N 是数组长度,S 是字符串长度。

空间复杂度 – O(S),因为我们需要在修改字符串后存储字符串。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程