char * countAndSay(int n){ int i,j,cnt,len; char buffer[100]={0}; char* pre = (char*)calloc(10000,sizeof(char)); sprintf(pre,"%d",1); for (i=2; i<=n; i++) { cnt=0; len=strlen(pre); char* retStr = (char*)calloc(10000,sizeof(char)); for(j=0; j<len; j++) { cnt++; if (j == strlen(pre)-1 || pre[j] != pre[j+1]) { sprintf(buffer,"%d",cnt); buffer[1]=pre[j]; strcat(retStr,buffer); cnt=0; } } pre=retStr; } return pre; }