两种方法翻转一个整数。顺序翻转和递归翻转

这里没考虑overflow的情况

递归的作用是使得反向处理。即从递归栈的最低端開始处理。通过绘图可得。

假设是rec(num/10):

12345

1234

123

12

1         <-- 递归使得这个最先处理

package recursion;

public class Reverse_digits_of_a_number {

	public static void main(String[] args) {
int num = 123;
System.out.println(reverse(num));
System.out.println(rec(num));
} public static int reverse(int num) {
int rev = 0;
while(num != 0) {
rev = rev * 10 + num % 10;
num /= 10; // /10相当于10进制的右移一位
} return rev;
} static int revNum = 0;
static int base = 1; public static int rec(int num) {
if(num == 0) {
return num;
} rec(num/10); // 相当于block在这里。直到递归究竟,eg,num=123 -> 12 -> 1 -> 0
revNum = revNum + base*(num%10);
base *= 10;
return revNum;
}
}

http://www.geeksforgeeks.org/write-a-c-program-to-reverse-digits-of-a-number/

翻转整数 Reverse digits of a number的更多相关文章

  1. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  2. 7. Reverse Integer(翻转整数)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  3. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  4. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  5. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  6. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  7. [Swift]LeetCode7. 反转整数 | Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  8. 【LeetCode】Reverse digits of an integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...

  9. [Swift]LeetCode25. k个一组翻转链表 | Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

随机推荐

  1. 【Hibernate】双向多对多Set查询

    一个计划对于多个竞价,一个竞价对应多个计划. 1.实体 /** * @author Tidy * @Description 计划 */ public class EbgStockPlanContent ...

  2. c++,派生类对象可以对基类赋值,基类对派生类不可以赋值

    派生类对象可以对基类对象赋值,赋值时属于派生类独有的部分就舍弃不用. #include <iostream> using namespace std; class DemoA { publ ...

  3. CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问

    参考文献: 1. NginxV1.8.0安装与配置 2. CentOS下在Nginx中添加SSL证书以支持HTTPS协议访问 3. nginx配置ssl证书的方法 4.nginx强制使用https访问 ...

  4. java计算两个日期相差多少天

    java计算两个日期相差多少天 public class DateUtil{ public static int betweenDays(Date startDate, Date endDate ) ...

  5. 基于visual Studio2013解决C语言竞赛题之0521圆盘求和

     题目

  6. Linux c c++ 开发调试技巧

    看到一篇介绍 linux c/c++ 开发调试技巧的文章,感觉挺使用,哪来和大家分享. 通向 UNIX 天堂的 10 个阶梯Author: Arpan Sen, 高级技术人员, Systems Doc ...

  7. 制作Linux(Fedora、Ubuntu、CentOS)优盘启动

    随着嵌入式技术的快速发展,Linux快速发展过一段时间.虽然现在不是很热,但是linux在现实社会的使用还是很有用处.而光盘有有些落伍,不仅浪费而且不环保,所以质优价廉的优盘就脱颖而出.所以,用优盘制 ...

  8. 如何A掉未来程序改

    话说有这样一道神题:[集训队互测2015]未来程序·改. 大意是要求写一个简单的C++解释器!这里去掉了C++的许多特性,连简单的break和continue都没有了! 话说NOI被屠了之后,一时心血 ...

  9. block 解析 - block变量

    block变量 上一篇 讲的是block静态变量的特性,这里我们来看一下_block变量.引用官方: You can specify that an imported variable be muta ...

  10. FineReport实现Java报表主题分析的效果图

    Java报表-財务主题-EVA经济附加 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVzdF9yZXBvcnQ=/font/5a6L5L2T/font ...