给定一个整数,我如何使用相同的数字找到它的下一个最大整数 [英] given an integer,how can i find the next largest integer of it using the same digits

查看:103
本文介绍了给定一个整数,我如何使用相同的数字找到它的下一个最大整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我有一个类似345的整数.我想编写一个函数,该函数将接受我可以做的整数,然后返回354.我知道我可以使用模运算符(%)提取最后一位数字5.但是然后我需要用4交换值并将4放在我可以做的最后一个位置.但是毕竟,我不知道如何将所有数字放回去并返回完整的整数,即354.plz帮助我

for example lets say i have an integer like 345. i want to write a function that will accept the integer which i can do, and return 354.i know i can extract the last digit 5 with a modolus operator(%) but then i need to swap the value with 4 and place 4 in the last position which i can do.But after all this,i dont know how to put at all the numbers back and return the completed integer which is 354.plz help me.It is an assignment for school.

推荐答案

为什么不给出提示:

您知道如何通过取模获得最后一位数字.

345/10(所有值都是整数,包括结果!)是多少?如果您不知道答案,请对其进行调试.然后,再想一想您对模数的了解.

依此类推,其他数字依此类推.

交换正确的数字后,重新计算您的数字d1 + d2 * 10 + d3 * 100 +....当然,您将循环执行此操作.

问候.
Why not give a hint:

You know how to get the last digit by modulo.

How much is 345 / 10 (all values integer, including the result!)? If you don''t know the answers debug it. And after that think again what you already know about modulo.

And so on and on and on for the other digits.

And after swapping the right digits recalculate your number d1 + d2 *10 + d3 *100 + .... Of course you will do that in a loop.

Regards.


这可以帮助您开发算法
this may help you to develop the algo http://www.ardendertat.com/2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/[^]


可能适用于像354 0r 454这样的数字,但是对于像3456这样的大数字呢? 0r 4567.iam真的很困惑吗???
that might work for a number like 354 0r 454,but what about bigger numbers like 3456 0r 4567.iam really confused????


这篇关于给定一个整数,我如何使用相同的数字找到它的下一个最大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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