分割数字并存储在int数组中 [英] splitting a number and storing in int array
本文介绍了分割数字并存储在int数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法拆分数字和存储数字在一个int数组?
Is there a way to split a number and store digits in an int array?
我正在寻找一种方法来删除一些数字可分割算法证明)。
I am looking for a way to remove some digits from a number (for a divisible algorithm proof).
例如,如果我有一个数字12345,我需要执行此操作:
for example, if I have a number 12345, I need to perform this operation:
1234 - 5 = 1229
1234 - 5 = 1229
有办法吗?
推荐答案
使用 n%10
获取最后一位数字,使用 n / 10
例如,5 = 12345%10,1234 = 12345/10。
Use n % 10
to get the last digit and n / 10
to get the others. For example, 5=12345%10, 1234=12345/10.
int array[6];
int n = 123456;
for (int i = 5; i >= 0; i--) {
array[i] = n % 10;
n /= 10;
}
一般来说,向量
int n = 123456;
vector<int> v;
for(; n; n/=10)
v.push_back( n%10 );
然后 v
包含 {6,5,4,3,2,1}
。您可以选择使用 std :: reverse
来撤消它。
Then v
contains {6,5,4,3,2,1}
. You may optionally use std::reverse
to reverse it.
这篇关于分割数字并存储在int数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文