您的位置 首页 编程语言

decodeArray

A number can be encoded as an integer array as follows. The first element of the array is any number and if it is negative then the encoded number is negative. Each digit of the number is the absolute value of the difference of two adjacent elements of the array. The most significant digit of the number is the absolute value of the difference of the first two elements of the array. For example, the array {2, -3, -2, 6, 9, 18} encodes the number 51839 because
5 is abs(2 – (-3))
1 is abs(-3 – (-2))
8 is abs(-2 – 6)
3 is abs(6-9)
9 is abs(9-18)
The number is positive because the first element of the array is >= 0.
If you are programming in Java or C#, the function prototype is int decodeArray(int[ ] a)
If you are programming in C or C++, the function prototype is
int decodeArray(int a[ ], int len) where len is the length of array a;
You may assume that the encoded array is correct, i.e., the absolute value of the difference of any two adjacent elements is between 0 and 9 inclusive and the array has at least two elements.

 

java 实现代码:

package com.zzy;

/**
 * 更多请关注: http://huamaodashu.com
 * Created by huamaodashu on 31/07/2018.
 */
public class decodeArray {
    public static void main(String[] args) {

//        int[] a = {0, -3, 0, -4, 0};
//        int[] a = {-1, 5, 8, 17, 15};
//        int[] a = {1, 5, 8, 17, 15};
//        int[] a = {111, 115, 118, 127, 125};
        int[] a = {1, 1};

        System.out.println(decodeArray(a));

    }
    public static  int decodeArray(int[] a){

        int sum =0;
        if(a.length>=0){
            for (int i = 0; i < a.length-1; i++) {

                sum = sum *10 + Math.abs(a[i]-a[i+1]);
            }

            if(a[0]<0){
                sum = -sum;
            }
            return sum;
        }else {
            return Integer.parseInt(null);
        }
    }

}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。