关闭

038 - Count and Say

89人阅读 评论(0) 收藏 举报
分类:

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.



数和说,这题目够无聊的

比如1211 我们会说1个1, 1个2, 2个1 既转换车概念111221


char* countAndSay(int n) 
{
	char *ret = (char *)calloc(sizeof(char), 8192);
	char *tmp = (char *)calloc(sizeof(char), 8192);
	char buff[8];
	ret[0] = '1';
	if (n == 1) return ret;
	n--;
	while (n--) {
		char *p = ret;
		char *move;
		char *pp = tmp;
		while (*p) {
			char say = p[0];
			int count = 0;
			move = p;
			while (move[count] == *p) {
				count++;
			}
			snprintf(buff, 8, "%d%c", count, say);
			strcat(tmp, buff);
			p += count;
		}
		memcpy(ret, tmp, strlen(tmp));
		tmp[0] = 0;
	}
	free(tmp);
	return ret;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28560次
    • 积分:1508
    • 等级:
    • 排名:千里之外
    • 原创:124篇
    • 转载:10篇
    • 译文:0篇
    • 评论:0条
    博客专栏