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

这里没考虑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. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  2. BZOJ 3275: Number( 最小割 )

    S->每个奇数,每个偶数->T各连一条边, 容量为这个数字.然后不能同时选的两个数连容量为+oo的边. 总数-最大流即是答案. 因为满足a2+b2=c2的a,b一定是一奇一偶或者两个偶数, ...

  3. Android广播——短信拦截

    MainActivity.java package com.example.broadcasttest; import android.content.Intent; import android.c ...

  4. 提高你开发效率的十五个Visual Studio 2010使用技巧

    提高你开发效率的十五个Visual Studio 2010使用技巧 相信做开发的没有不重视效率的.开发C#,VB的都知道,我们很依赖VS,或者说,我们很感谢VS.能够对一个IDE产生依赖,说明这个ID ...

  5. php5.3升级到5.5

    在网站中发布中: / 开启调试模式 建议开发阶段开启 部署阶段注释或者设为falsedefine('APP_DEBUG',true); true没问题,改为:false就报错 报错如下: PHP Fa ...

  6. 2014 HDU多校弟五场A题 【归并排序求逆序对】

    这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...

  7. C# 7.0

    C# 7.0 本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性 ...

  8. hdu 1251 统计难题 初识map

    Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是 ...

  9. [产值分析]生产部KPI考核之产值分析

    接到新任务:设计统计电子和磁电公司生产部产值分析报表. 眼下状况: 1.电子公司:取最新单位价格*入库数量 2.磁电公司:取最低价格*入库数量(实际取价的时候又没有取到最低价) 假设计算出来的结果和財 ...

  10. PHP - session编码和解码

    <?php //session编码 session_start(); $_SESSION['name'] = 'Jack'; $_SESSION['sex'] = 'men'; $envar = ...