提取整数的数字元素
extracting the digital elements of an integer
我想知道如何获得整数的数字元素,以便显示一个数组,例如来自数字17的[1][7]
。我在这里找到了这个计算数字的解决方案,现在我需要将组成整数的数字放入一个数组中,所以我想检索每个数字的值
int A = 17;
int table[lenght_of_A];
A 'operation_to_fit_A_in' table;
在C++中,我会这样做。那么就不需要事先对数字进行计数:
#include <list>
std::list<int> digits;
// Slightly adapted algorithm from sharptooth: this one yields a zero if
// the value was 0. (sharptooth's wouldn't yield the digit 0 if a zero was
// being analyzed.)
do {
digits.push_front( value%10 );
value /= 10;
} while( value!=0 );
digits
现在包含一个单独数字的列表,并且可以以您喜欢的任何形式显示。
这个想法是运行一个循环(伪代码):
while( value != 0 ) {
nextDigitValue = value % 10; // this is "modulo 10"
nextDigitChar = '0' + nextDigitValue;
value = value / 10; // this lets you proceed to next digit
}
void Getint(int A; int* array)
{
if (A < 10)
array[0] = A;
array[1] = 0;
if (A >= 10 && A < 100)
array[0] = A / 10;
array[1] = A % 10;
}
相关文章:
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 为什么使用数组元素查找最大数字的程序不起作用?
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 如何非常快速地将数字添加到 Vector 中的一系列元素中
- 我应该如何使用remove_if删除两个数字范围内的元素
- 查找其元素总和为矩阵中给定数字的路径
- 将数字的字符串数组转换为整数数组时,元素将变为 0
- 每个元素小于某个特定数字的子数组的总和
- 计算向量中大于数字的元素
- 如何将字符串的元素用作数字
- 如何有效地找到数字流中元素的等级
- 如何在向量中找到大于某些数字的所有元素
- 当矢量有 6 个元素(C++、矢量、循环)时,程序打印异常数字
- 如何将向量中的所有元素替换为元素数字的总和
- 将数组的所有元素初始化为相同的数字
- C 如何从向量到给定数字的向量中找到最小元素数量
- 最小元素是特定给定数字的子阵列数量
- 返回连续数字的逻辑假设数组包含元素 1,9,2,3,4,9,6,7,8,那么它应该返回 2,3,4,6,7,8
- 给定分隔成数组元素的数字,如何在 int 中重新组合数字