本帖最后由 OCTSJimmy 于 2014-11-9 11:17 编辑
问题是这样子的,
程序执行后假如都输入数字30,会——输出一堆的空格,但是,将这些字符串复制出来,粘贴在记事本中,会发现又正常了,这是?……
感觉好神奇好高端有木有?……
代码如下:
注释是一样的,又长,就不多帖了。
- <P>/*
- * “伯爵说”序列如下:1, 11, 21, 1211, 111221, ...
- * 1 读作 "一(个)1" 由此可写成 11
- * 11 读作 "二(个)1" 由此可写成 21
- * 21 读作 "一(个)2, 一(个)1" 或者 1211.
- *
- * The count-and-say sequence is the sequence of integers beginning as follows:
- * 1, 11, 21, 1211, 111221, ...
- *
- * 1 is read off as "one 1" or 11.
- * 11 is read off as "two 1s" or 21.
- * 21 is read off as "one 2, then one 1" or 1211.
- *
- * Given an integer n, generate the nth sequence.
- * Note: The sequence of integers will be represented as a string.</P>
- <P> *
- * 格式:给定一个整数n,输出第n个序列。
- * 注意:整数序列以字符串的形式表示。
- * PS:相信你已经看懂了题目,如果没看懂,小提示下,
- * 其实类似于求”菲波拉契“数列的第n项哦~
- * 样例输入
- * 6
- * 样例输出
- * 312211
- */</P>
复制代码 代码递归版本的:
- import java.util.Scanner;
- public class Main2 {
- private static StringBuilder[] arrString = new StringBuilder[100];
- private static Scanner sc = new Scanner(System.in);
- private static int getInput(){
- int num = sc.nextInt();
- return num;
- }
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- String str = getResult(getInput());
- str = str.substring(0, str.length()-1);
- System.out.println(str);
- }
- private static String getResult(int in){
- for(int i=0;i<100;i++){
- arrString[i] = new StringBuilder();
- }
- return coreDo(in).toString();
- }
- private static StringBuilder coreDo(int in){
- StringBuilder str = new StringBuilder();
- if(in < 2){
- return new StringBuilder().append("1 ");
- }
- if(in<3){
- return new StringBuilder().append("11 ");
- }
- if(in < 100){
- if(arrString[in].length() > 0){
- return arrString[in];
- } else {
- str = coreDo(in -1);
- arrString[in] = str;
- }
- } else {
- str = coreDo(in -1);
- }
- StringBuilder result = new StringBuilder();
- boolean sign = false;
- char ch1 = 0, ch2 = 0;
- for(int i=0;i<str.length();i++){
- ch1 = str.charAt(i);
- for(int j=i;j<str.length();j++){
- ch2 = str.charAt(j);
- if(i == j){
- continue;
- }
- if(ch2==ch1){
- if(sign){
- result.append("3");
- result.append(str.charAt(j));
- i = j;
- sign = false;
- break;
- } else {
- sign = true;
- }
- } else {
- if(sign){
- result.append("2");
- result.append(str.charAt(j-1));
- i = j - 1;
- sign = false;
- break;
- } else {
- i = j - 1;
- result.append("1");
- result.append(str.charAt(j-1));
- break;
- }
- }
- }
- }
- return result.append(" ");
- }
- }
复制代码 非递归版本的:
- import java.util.Scanner;
- public class Main {
- private static StringBuilder[] arrString = new StringBuilder[100];
- private static Scanner sc = new Scanner(System.in);
- private static int getInput(){
- int num = sc.nextInt();
- return num;
- }
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- String str = getResult(getInput());
- str = str.substring(0, str.length()-1);
- System.out.println(str);
- }
- private static String getResult(int in){
- for(int i=0;i<100;i++){
- arrString[i] = new StringBuilder();
- }
- StringBuilder sb = new StringBuilder();
- for(int i=0;i<=in;i++){
- sb = coreDo(i, sb);
- }
- return sb.toString();
- }
- private static StringBuilder coreDo(int in, StringBuilder preSb){
- StringBuilder str = new StringBuilder();
- if(in < 2){
- return new StringBuilder().append("1 ");
- }
- if(in<3){
- return new StringBuilder().append("11 ");
- }
- if(in < 100){
- if(arrString[in].length() > 0){
- return arrString[in];
- } else {
- str = preSb;
- arrString[in] = str;
- }
- } else {
- str = preSb;
- }
- StringBuilder result = new StringBuilder();
- boolean sign = false;
- char ch1 = 0, ch2 = 0;
- for(int i=0;i<str.length();i++){
- ch1 = str.charAt(i);
- for(int j=i;j<str.length();j++){
- ch2 = str.charAt(j);
- if(i == j){
- continue;
- }
- if(ch2==ch1){
- if(sign){
- result.append("3");
- result.append(str.charAt(j));
- i = j;
- sign = false;
- break;
- } else {
- sign = true;
- }
- } else {
- if(sign){
- result.append("2");
- result.append(str.charAt(j-1));
- i = j - 1;
- sign = false;
- break;
- } else {
- i = j - 1;
- result.append("1");
- result.append(str.charAt(j-1));
- break;
- }
- }
- }
- }
- return result.append(" ");
- }
- }
复制代码
程序执行后假如都输入数字30,会——输出一堆的空格,但是,将这些字符串复制出来,粘贴在记事本中,会发现又正常了,这是?……
感觉好神奇好高端有木有?……
|