我正在寻找一种高效且简单的方法将整数分隔为数字,但我需要从第一个开始将它们分开。将整数分成非数字的位数
下面是sepparate一个数字,数字的典型方式:
int main(int argc, char* argv[]) { //argc is going to be the number
while(argc != 0) {
cout << argc % 10 << endl;
argc /= 10;
}
return 0;
}
但是做这种方式,我会得到,例如,5437 - > 7,3,4,5 我想扭转上诉顺序,5,4,3,7。所以我创造了这个:
int main(int argc, char* argv[]) {
int rem = 1;
while(argc/rem > 10) //We increase the remainder until we get the same amount of digits than the number
rem *= 10;
while(argc != 0) {
cout << argc/rem << endl; //We take the cocient of the division, wich will be always the first digit
argc %= rem; //Now we discard that digit
rem /= 10; //And we decrease the number of digits of the remainder too
}
return 0;
}
事情是:是否有任何其他方式以较短/更简单的方式做到这一点?
PS:我不能使用堆栈,列表或那种结构。
在此先感谢
递归是另一种方式。 –
我通常只有一个数组存储,然后将其向前打印。 –
我无法想象不得不限制自己使用堆栈或列表,除非汇编语言是真正的目标。在6502中有各种各样的小技巧可以使用。在缺少本地分割指令的多台机器中,存在一种涉及三位移位的技巧,并根据何时添加来添加3或6。 6的意义在于它当然是10和16之间的差别。穆罕默德的答案看起来就像你应该使用的答案,即使“技术上”数组是一个堆栈。 –