POJ3438 ZOJ2886 UVALive3822 Look and Say【数列+水题】

 

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9475 Accepted: 5773

 

Description

The look and say sequence is defined as follows. Start with any string of digits as the first element in the sequence. Each subsequent element is defined from the previous one by "verbally" describing the previous element. For example, the string 122344111 can be described as "one 1, two 2's, one 3, two 4's, three 1's". Therefore, the element that comes after 122344111 in the sequence is 1122132431. Similarly, the string 101 comes after 1111111111. Notice that it is generally not possible to uniquely identify the previous element of a particular element. For example, a string of 112213243 1's also yields 1122132431 as the next element.

Input

The input consists of a number of cases. The first line gives the number of cases to follow. Each case consists of a line of up to 1000 digits.

Output

For each test case, print the string that follows the given string.

Sample Input

3
122344111
1111111111
12345

Sample Output

1122132431
101
1112131415

Source

 

 

 

Regionals 2007 >> North America - Rocky Mountain

 

问题链接POJ3438 ZOJ2886 UVALive3822 Look and Say

问题简述:(略)

问题分析

  这个Look and Say数列在OEIS中的编号是A005150

  这个问题是首先输入测试例子数量t,然后输入t行数字串,将每一行的数字串转换为Look and Say数字串输出。

程序说明

  这里给出两个程序,一个是将字符串读入到字符数组中,然后进行将数字串转换为Look and Say数字串的处理;另外一个程序是一边读入字符串,一边处理,省去缓冲存储。

  使用getchar()函数直接处理输入流的话,不使用多余的缓存,是一个好主意。

 

 

参考链接I00031 Look-and-say sequence

 

AC通过的C语言程序(正解)如下:

/* POJ3438 ZOJ2886 Look and Say */

#include <stdio.h>

int main(void)
{
    int t, say;
    char look, c;

    scanf("%d", &t);
    getchar();
    while(t--) {
        // Look and Say转换:一边读入一边转换
        look = getchar();   // 读入首字符
        say = 1;
        while((c = getchar()) != '\n') {
            if(c == look)
                say++;
            else {
                printf("%d%c", say, look);

                look = c;
                say = 1;
            }
        }
        printf("%d%c\n", say, look);
    }

    return 0;
}

 

AC通过的C语言程序如下:

 

/* POJ3438 ZOJ2886 Look and Say */

#include <stdio.h>

#define MAXN 1024

char r[MAXN * 2];

int main(void)
{
    int t, say;
    char look, c, *pt, temp[10], *pt2;

    scanf("%d", &t);
    getchar();
    while(t--) {
        // Look and Say转换:一边读入一边转换
        pt = r;
        look = getchar();   // 读入首字符
        say = 1;
        while((c = getchar()) != '\n') {
            if(c == look)
                say++;
            else {
                sprintf(temp, "%d", say);
                pt2 = temp;
                while(*pt2)
                    *pt++ = *pt2++;
                *pt++ = look;

                look = c;
                say = 1;
            }
        }
        sprintf(temp, "%d", say);
        pt2 = temp;
        while(*pt2)
            *pt++ = *pt2++;
        *pt++ = look;
        *pt = '\0';

        // 输出结果
        printf("%s\n", r);
    }

    return 0;
}

 

 

 

另外一个AC通过的C语言程序如下:

 

/* POJ3438 ZOJ2886 Look and Say */

#include <stdio.h>

#define MAXN 1024

char s[MAXN];
char r[MAXN * 2];

int main(void)
{
    int t, say;
    char look, *ps, *pt, temp[10], *pt2;

    scanf("%d", &t);
    while(t--) {
        // 读入字符串
        scanf("%s", s);

        // Look and Say转换
        ps = s;
        pt = r;
        look = *ps;
        say = 1;
        while(*(++ps)) {
            if(*ps == look)
                say++;
            else {
                sprintf(temp, "%d", say);
                pt2 = temp;
                while(*pt2)
                    *pt++ = *pt2++;
                *pt++ = look;

                look = *ps;
                say = 1;
            }
        }
        sprintf(temp, "%d", say);
        pt2 = temp;
        while(*pt2)
            *pt++ = *pt2++;
        *pt++ = look;
        *pt = '\0';

        // 输出结果
        printf("%s\n", r);
    }

    return 0;
}

 

 

 

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值