曖殇

博客园 首页 联系 订阅 管理

Look-and-say sequence is a sequence of integers as the following:

D, D1, D111, D113, D11231, D112213111, ...

where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1's, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.

Input Specification:

Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (≤ 40), separated by a space.

Output Specification:

Print in a line the Nth number in a look-and-say sequence of D.

Sample Input:

1 8

Sample Output:

1123123111
题意:
表示数字 比如1 一个一 11 两个一 12 一个一,一个二 1121 ....
其实直接暴力就可以(虽然我一直认为n=40的话会是2^40,实际上才63139)
用到strcpy()函数,头文件是cstring,如果不知道那就用指针咯,还快,只需要改变两个指针指向就OK了。
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
	char a;
	int n;
	cin>>a>>n;
	char qqq[100000];        //char arr[100000];
	char www[100000];    //char crr[100000];
	char *arr=qqq;
	char *crr=www;
	char brr[10]={'0','1','2','3','4','5','6','7','8','9'};  //其实个数不可能太多4 5个都是极限
	arr[0]=a;
	arr[1]='\0';
	for(int i=1;i<n;i++)
	{
		int j=0;
		int z=0;
		int q=1;
		while(arr[j]!='\0')
		{
			if(arr[j]==arr[j+1])
			{
				q++;
			}
			else
			{
				crr[z++]=arr[j];
				crr[z++]=brr[q];
				q=1;
			}
			j++;
		}
		crr[z++]='\0';
		char *aaa=arr;    //strcpy(arr,crr);
		arr=crr;
		crr=aaa;
	 }
	 printf("%s",arr);
}

  

posted on 2018-12-08 17:53  曖殇  阅读(158)  评论(0编辑  收藏  举报