The question is not intuitive, here the description is better some http://www.careercup.com/question? Id = 4425679 "Count and Say problem" Write a code to do following: n String to print 0 1 1 1 1 2 2 1 3 1 2 1 1... base case: n = 0 print "1" for n = 1, look at previous string and write number of times a digit is seen and the digit itself. in this case, digit 1 is seen 1 time in a row... so print "1" for n = 2, digit 1 is seen two times in a row, so print "2 1" for n = 3, digit 2 is seen 1 time and then digit 1 is seen 1 so print "1 2 1" for n = 4 you will print "1 1 1 2 2 1" Consider the numbers integers for simplicity. e.g. if previous string is "10 1" then the next will be "1 10 1" and the next one will be "1 1 1 10 2 1"
Package Level2;/*** Count and Say ** The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21,121 1, 111221 ,... 1 is read off as "one 1" or 11. 11 is read off as "two 1 s" 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. **/public class S38 {public static void main (String [] args) {System. out. println (countAndSay (6);} public static String countAndSay (int n) {if (n = 1) {return "1";} String s = "1 "; stringBuffer ret = new StringBuffer (); int cnt = 0; int round = 0; // number of round iterations int I; while (++ round <n) {cnt = 1; ret. setLength (0); for (I = 1; I <s. length (); I ++) {if (s. charAt (I) = s. charAt (I-1) {// repeat value, continue counting cnt ++;} else {// new value appears, record to ret. append (cnt ). append (s. charAt (I-1); cnt = 1; // reset cnt} ret. append (cnt ). append (s. charAt (I-1); s = ret. toString (); // update s} return ret. toString ();}}