Counting the number and counting of garlic guests (implemented in C)

Source: Internet
Author: User

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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.