Look-and-say数列是一个有趣的数列。有关介绍参见维基百科的Look-and-say sequence。在OEIS网站上,数列号为A005150。
这个程序打印输出Look-and-say数列的前18项。
C语言程序如下:
/* I00031 Look-and-say sequence */ #include <stdio.h> #include <memory.h> #define MAXN 100000 char s[MAXN+1], t[MAXN+1]; char *ps, *pt; int main(void) { char look; int say, i; memset(s, 0, sizeof(s)); memset(t, 0, sizeof(t)); s[0] = '1'; for(i=0; i<18; i++) { if(i & 1) { ps = t; pt = s; } else { ps = s; pt = t; } printf("%s ", ps); look = *ps; say = 1; while(*(++ps)) { if(*ps == look) say++; else { *pt++ = '0' + say; *pt++ = look; look = *ps; say = 1; } } *pt++ = '0' + say; *pt++ = look; } return 0; }
运行结果如下:
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211
311311222113111231131112132112311321322112111312211312111322212311322113212221
132113213221133112132113311211131221121321131211132221123113112221131112311332111213211322211312113211
11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221
31131122211311123113321112131221123113112211121312211213211321322112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211