Counting the number and counting of garlic guests (implemented in C)
The "count" sequence is as follows: 1, 11, 21,121 1, 111221,... 1 read as "one 1" or 11. 11 read as "two 1 s" or 21. 21 read as "one 2, one 1" or 1211.
Format: Multiple Input groups, read to the end of the file. Each input group is given an integer n and the nth sequence is output. (1 <= n <= 30)
Note: The integer sequence is represented as a string.
Rule:
1 is composed of 1
11 is composed of two shards.
21 is composed of one 2 and one 1
1211 consists of 1, 1, 2, and 1
111221...
1 #include <stdio.h> 2 #include <string.h> 3 void func(char *s, int n); 4 int main() 5 { 6 int n; 7 char s[10000]; 8 while (scanf("%d", &n)!=EOF){ 9 strcpy(s, "1");10 func(s, n);11 printf("%s\n", s);12 } 13 return 0;14 } 15 void func(char *s, int n)16 {17 int i, j, count, len;18 char ch, tmp[10000];19 for (j = 1; j < n; ++j) {20 len = 0;21 for (i = 0; s[i] != '\0'; ++i) {22 if (i == 0) {23 ch = s[i];24 count = 1;25 }26 else {27 if (ch == s[i])28 ++count;29 else {30 len += sprintf(tmp+len, "%d%c", count, ch);31 ch = s[i];32 count = 1;33 }34 }35 }36 sprintf(tmp+len, "%d%c", count, ch);37 strcpy(s,tmp);38 }39 }