One algorithm question per day-simple-calculate an integer's reverse sequence integer.

Source: Internet
Author: User

For example, an integer of 1234 and an integer of 4321 is output. The problem is excerpted from <C language example development>

Algorithm Implementation is not good enough.

package com.jue.numberreverse;public class NumberReverse {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint old = 123456789;System.out.println("old:" + old);System.out.println("new:" + getReverseDataNumber(old));}public static int getReverseDataNumber(int old) {final int length = getDataLength(old);final int[] datas = new int[length];int tempdata = old;for (int i = 0; i < length; i++) {int dataInIndex = tempdata / getMultipleData(length - 1 - i);datas[i] = dataInIndex;tempdata -= dataInIndex * getMultipleData(length - 1 - i);}int newdata = 0;for (int i = 0; i < datas.length; i++) {newdata += datas[i] * getMultipleData(i);}return newdata;}private static int getMultipleData(int indexInNumber) {if (indexInNumber == 0) {return 1;}return 10 * getMultipleData(indexInNumber - 1);}public static int getDataLength(int data) {return String.valueOf(data).length();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.