I00031 Look-and-say sequence

2024-04-08 23:58
文章标签 say sequence look i00031

本文主要是介绍I00031 Look-and-say sequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



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 100000char 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\n", 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




这篇关于I00031 Look-and-say sequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/886707

相关文章

Unity3D自带Mouse Look鼠标视角代码解析。

Unity3D自带Mouse Look鼠标视角代码解析。 代码块 代码块语法遵循标准markdown代码,例如: using UnityEngine;using System.Collections;/// MouseLook rotates the transform based on the mouse delta./// Minimum and Maximum values can

浙大数据结构:02-线性结构4 Pop Sequence

这道题我们采用数组来模拟堆栈和队列。 简单说一下大致思路,我们用栈来存1234.....,队列来存输入的一组数据,栈与队列进行匹配,相同就pop 机翻 1、条件准备 stk是栈,que是队列。 tt指向的是栈中下标,front指向队头,rear指向队尾。 初始化栈顶为0,队头为0,队尾为-1 #include<iostream>using namespace std;#defi

【UVA】1626-Brackets sequence(动态规划)

一道算是比较难理解的动规。 状态转移分2个: (用d[i][j]表示在i~j内最少需要添加几个括号,保持平衡) 1.如果s[i]和s[j]是一对括号,那么d[i][j] = d[i + 1][j - 1] 2.否则的话 d[i][j] = min(d[i][k],[k + 1][j]); 边界是d[i + 1][i] = 0; d[i][i] = 1; 13993644 162

【UVA】10534 - Wavio Sequence(LIS最长上升子序列)

这题一看10000的数据量就知道必须用nlog(n)的时间复杂度。 所以特意去看了最长上升子序列的nlog(n)的算法。 如果有2个位置,该位置上的元素为A[i]和A[j],并且他们满足以下条件: 1.dp[i] = dp[j]    (dp[x]代表以x结尾的最长上升子序列长度) 2.A[i] < A[j] 3.i < j 那么毫无疑问,选择dp[i] 一定优于选择dp[j] 那么

leetcode#38. Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 12. 113. 214. 12115. 111221 1 is read off as “one 1” or 11. 11 is read off

2015年多校联合训练第一场OO’s Sequence(hdu5288)

题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个对应的j使得a[i]%a[j]=0,那么l,r内有多少个i,f(l,r)就是几。问所有f(l,r)的总和是多少。 公式中给出的区间,也就是所有存在的区间。 思路:直接枚举每一个数字,对于这个数字,如果这个数字是合法的i,那么向左能扩展的最大长度是多少,向右能扩展的最大长度是多少,那么i为合法的情况

NYOJ 763 Vawio Sequence

OJ题目 : 戳这里~ 描述 Vawio Sequence is very funny,it is a sequence of integers. It has some interesting properties. ·   Vawio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of  Vawio s

python中使用FormatDataLibsvm转为txt文件后报错illegal multibyte sequence

‘gbk’ codec can’t decode byte 0xff in position 0: illegal multibyte sequence 这个报错是因为编码不对,正确的编码是ANSI编码,txt文件打开后另存为可以看到当前的文本文档编码 但是excel不能直接保存ANSI编码的txt文件 所以不能直接保存为ANSI编码 有两种解决办法 1.新建一个txt文件(新建的txt文件

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

LeetCode - 38. Count and Say

38. Count and Say  Problem's Link  ---------------------------------------------------------------------------- Mean:  题目意思太晦涩。 1 读出来 就是“1个1” 所以记为“11” 11 读出来 就是“2个1” 所以记为“21” 21 读出来 就是“1个