报数420
/***
* @ClassName: numberOff420
* @Description:
* 報數指的是,按照其中的整數的順序進行報數,然后得到下一個數。如下所示:1, 11, 21, 1211, 111221, ...1 讀作 "one 1" -> 11.11 讀作 "two 1s" -> 21.21 讀作 "one 2, then one 1" -> 1211.給定一個整數 n, 返回 第 n 個順序。
* @author laiwufa
* @date 2016年2月19日
**/
public class numberOff420 {public static void main(String[] args) {System.out.println(countAndSay(5));}public static String countAndSay(int n) {String oldString = "1";while (--n > 0) {StringBuilder sb = new StringBuilder();char [] oldChars = oldString.toCharArray();for (int i = 0; i < oldChars.length; i++) {int count = 1;while ((i+1) < oldChars.length && oldChars[i] == oldChars[i+1]) {count++;i++;}sb.append(String.valueOf(count) + String.valueOf(oldChars[i]));}oldString = sb.toString();}return oldString;}
}
?
轉載于:https://www.cnblogs.com/zilanghuo/p/5201624.html
總結
- 上一篇: 用字节流查看txt文件
- 下一篇: 【BZOJ4254】Aerial Tra