递归......很难掌握。救命。 [英] Recursion...Its hard to grasp. Help.

查看:76
本文介绍了递归......很难掌握。救命。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以今晚我有我的算法课,我希望在课前或课堂上完成实验。我编写了一个递归方法,它将int作为参数并返回一个int。它必须REVERSE int。有没有搞错。我不允许把它转换成字符串或任何东西,它说我不会得到信任。有人可以给我一些提示吗?到目前为止,我唯一得到的是从用户接受int的main方法以及对reverse()方法的调用,该方法尚未编写。 K thx。

So I have my Algorithms class tonight and I want to get the lab done before or during the class. I have write a recursive method that takes an int as a parameter and also returns an int. It has to REVERSE the int. What the heck. I''m not allowed to convert it to a string or anything, it says that I will not get credit if I do. So could someone give me some hints? The only thing I''ve gotten so far is the main method that accepts an int from the user and the call to the reverse() method which hasn''t even been written yet. K thx.

推荐答案

是否允许两个(递归)方法?如果是这样,将数字视为HD,其中H是没有最后一个数字D的数字,D是最后一个数字。您想要反转H并在其前面添加单个数字D.这意味着你必须根据H的大小将D乘以10的幂。


这里是扰流板的一部分:

Are two (recursive) methods allowed? If so, think of a number as HD where H is the number without the last digit D and D is the last digit. You want to reverse H and prepend the single digit D to it. This means that you have to multiply D by a power of ten depending on the magnitude of H.

Here''s part of the spoiler:

展开 | 选择 | Wrap | 行号


谢谢Joe!我需要一段时间才能完全理解你刚写的内容,但是我打算将它打印出来并在我编写方法时参考它。就编写方法而言,我有很大的自由,所以我不明白为什么我不能使用两个。只要我没有将它转换成字符串ha!再次感谢乔。如果有其他人有任何建议请发帖!
Thanks Joe! It will take a while for me to fully comprehend what you just wrote, but I''m going to print it out and refer to it whilst I write the method. I have a good deal of freedom as far as writing the method goes so I dont see why I couldn''t use two. As long as I''m not converting it to a string ha! Thanks again Joe. If anyone else has any more suggestions please post!


好的,所以我现在对自己感觉非常好。我让程序运行得相当好。但是......点点点......反向1()方法应该消除任何零点。例如,1024应该变成421.但我不能想办法做到这一点。有人有主意吗?我要到下周二才弄明白。

Okay so I''m feeling really good about myself right now. I got the program to work fairly well. HOWEVER...dot dot dot...the reverse1() method is supposed to eliminate any zero''s. For example, 1024 should become 421. But I cant think of a way to do this. Anyone have any ideas? I have until next Tuesday to figure it out.

展开 | 选择 | Wrap | 行号


这篇关于递归......很难掌握。救命。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆