【问题标题】:Finding a Specific Digit of a Number查找数字的特定数字
【发布时间】:2023-03-21 04:50:01
【问题描述】:

我正在尝试查找任意长度整数的nth 位。我打算将整数转换为字符串并使用索引 n 处的字符...

char Digit = itoa(Number).at(n);

...但后来我意识到itoa 函数不是标准的。有没有其他方法可以做到这一点?

【问题讨论】:

  • sprintf? (C 而不是 C++,但它会工作:))

标签: c++ integer digit


【解决方案1】:

(number/intPower(10, n))%10

只需定义函数intPower

【讨论】:

    【解决方案2】:

    您还可以在循环中使用 % 运算符和 / 进行整数除法。 (给定整数 n >= 0,n % 10 给出个位,n / 10 去掉个位。)

    【讨论】:

      【解决方案3】:

      您可以使用 ostringstream 转换为文本字符串,但是 类似以下的函数:

      char nthDigit(unsigned v, int n)
      {
          while ( n > 0 ) {
              v /= 10;
              -- n;
          }
          return "0123456789"[v % 10];
      }
      

      应该以更少的复杂性来解决问题。 (为了 启动器,它处理 n 大于数字的情况 位数正确。)

      -- 詹姆斯·坎泽

      【讨论】:

        【解决方案4】:

        【讨论】:

          【解决方案5】:

          也可以通过函数 log10, int cmath 来避免转换为字符串,该函数返回一个数字的 10 基对数(大约是它的长度)如果是字符串):

          unsigned int getIntLength(int x)
          {
              if ( x == 0 )
                      return 1;
              else    return std::log10( std::abs( x ) ) +1;
          }
          
          char getCharFromInt(int n, int x)
          {
              char toret = 0;
              x = std::abs( x );
              n = getIntLength( x ) - n -1;
          
              for(; n >= 0; --n) {
                  toret = x % 10;
                  x /= 10;
              }
          
              return '0' + toret;
          }
          

          我已经对其进行了测试,并且效果很好(负数是一种特殊情况)。此外,必须考虑到,为了找到第 n 个元素,您必须在循环中向后“走”,从总 int length 中减去。

          希望这会有所帮助。

          【讨论】:

            【解决方案6】:

            直接回答是:

            char Digit = 48 + ((int)(Number/pow(10,N)) % 10 );
            

            您应该包含<math>

            【讨论】:

              【解决方案7】:
              number = 123456789
              n = 5
              
              tmp1 = (int)(number / 10^n);   // tmp1 = 12345
              tmp2 = ((int)(tmp1/10))*10;    // tmp2 = 12340
              digit = tmp1 - tmp2;           // digit = 5
              

              【讨论】:

              • 请注意,^ 在 C++ 中是位级 XOR。
              【解决方案8】:
              const char digit = '0' + number.at(n);
              

              假设number.at(n)返回一个0...9范围内的十进制数字,即。

              【讨论】:

              • 我无法将number 转换为字符串以使用.at(n)itoa 不是标准的。
              【解决方案9】:

              更通用的方法:

              template<int base>
              int nth_digit(int value, int digit)
              {
                  return (value / (int)pow((double)base, digit)) % base;
              }
              

              只是让您对不同的基数(例如 16、32、64 等)执行相同的操作。

              【讨论】:

                【解决方案10】:

                itoa 的替代方法是std::to_string 方法。所以,你可以简单地这样做:

                char digit = to_string(number)[index]
                

                【讨论】:

                • 需要越界处理。
                猜你喜欢
                • 2014-02-19
                • 1970-01-01
                • 2015-10-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-11-09
                相关资源
                最近更新 更多