Java 杂谈程序员快乐数学

神奇的ASCII码: 150-159

2019-03-26  本文已影响32人  Reone_JS

好神奇呀!

  1. 将字符串拆分,每个字节的ASCII码求和;
  2. 将此和转为字符串,重复步骤(1)
  3. 最终,无论什么字符串,都会变成 150-159 其中一个数字

java代码:

import java.util.ArrayList;
import java.util.List;

/**
 * Created by reone on 2019/3/26.
 */
public class StrTest {
    public static void main(String[] args) {
        List<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < 299999999; i++) {
            if (i == endNum(i)) {
                arrayList.add(i);
            }
        }
        System.out.println(arrayList);
        System.out.println(endNumStr("QWERTYUI哦啊三十多发光飞碟 !@#¥%……& 阿什顿飞机`"));
    }

    private static int endNum(int num) {
        int temp = num;
        while (true) {
            int newNum = cover(temp + "");
            if (newNum == temp) {
                break;
            } else {
                temp = newNum;
            }
        }
        return temp;
    }

    private static int cover(String str) {
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
            sum += str.charAt(i);
        }
        return sum;
    }

    private static int endNumStr(String str) {
        return endNum(cover(str));
    }
}

运行结果:

[150, 151, 152, 153, 154, 155, 156, 157, 158, 159]
151

Process finished with exit code 0

所以,我发现了什么?

下面是150-159的ascll码的分解

150:[1: 49]  [5: 53]  [0: 48]  
151:[1: 49]  [5: 53]  [1: 49]  
152:[1: 49]  [5: 53]  [2: 50]  
153:[1: 49]  [5: 53]  [3: 51]  
154:[1: 49]  [5: 53]  [4: 52]  
155:[1: 49]  [5: 53]  [5: 53]  
156:[1: 49]  [5: 53]  [6: 54]  
157:[1: 49]  [5: 53]  [7: 55]  
158:[1: 49]  [5: 53]  [8: 56]  
159:[1: 49]  [5: 53]  [9: 57] 
上一篇下一篇

猜你喜欢

热点阅读