识别给定数字中的数字。
我是编程新手,我遇到了一个问题。 我希望我的程序能够识别给定数字中的各个数字,例如如果我输入 4692
,它应该识别这些数字并打印 4 6 9 2
。 是的,不使用数组。
I'm new to programming, and I'm stuck at a problem. I want my program to identify the separate digits in a given number, like if I input 4692
, it should identify the digits and print 4 6 9 2
. And yeah, without using arrays.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您是编程新手,那么这是一个需要解决的完美递归问题...
4692/1000 = 4
4692%1000 = 692
692/100 = 6
692%100 = 92
92/10 = 9
92%10 = 2
您应该了解现在应该使用的循环,以便它适用于任何数字。 :)
A perfect recursion problem to tackle if you're new to programming...
4692/1000 = 4
4692%1000 = 692
692/100 = 6
692%100 = 92
92/10 = 9
92%10 = 2
You should get the idea for the loop you should use now, so that it works for any number. :)
已经好几年没写过 C 代码了,但这应该可以。
Haven't written C code in year, but this should work.
简单又好看
Simple and nice
另一种方法是有两个循环。
1)第一个循环:将数字反转。
2)第二次循环:从右到左打印数字。
这是假设您希望从右到左打印数字。 我注意到这里有几个解决方案没有这个假设。 如果没有,那么只需第二个循环就足够了。
Another approach is to have two loops.
1) First loop: Reverse the number.
2) Second loop: Print the numbers from right to left.
This is assuming you want the digits printed from right to left. I noticed there are several solutions here that do not have this assumption. If not, then just the second loop would suffice.
我认为这个想法是打印不重复的数字(否则就太简单了)......好吧,您可以跟踪已经打印的整数,而无需用数组将它们编码为另一个整数。
一些伪C,给你一个线索:
I think the idea is to have non reapeating digits printed (otherwise it would be too simple)... well, you can keep track of the already printed integers without having an array encoding them in another integer.
some pseudo C, to give you a clue:
如果您只想打印号码中的数字,这里有一个简单的解决方案。
Here is a simple solution if you want to just print the digits from the number.
这是对的吗
Is it correct