• 1140 Look-and-say Sequence PAT


    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]='';
    	for(int i=1;i<n;i++)
    	{
    		int j=0;
    		int z=0;
    		int q=1;
    		while(arr[j]!='')
    		{
    			if(arr[j]==arr[j+1])
    			{
    				q++;
    			}
    			else
    			{
    				crr[z++]=arr[j];
    				crr[z++]=brr[q];
    				q=1;
    			}
    			j++;
    		}
    		crr[z++]='';
    		char *aaa=arr;    //strcpy(arr,crr);
    		arr=crr;
    		crr=aaa;
    	 }
    	 printf("%s",arr);
    }
    

      

  • 相关阅读:
    ubuntu12.04 安装opencv
    VC warning C4786
    su root 后还是不能使用useradd ,useradd 等命令
    C++数组
    C++多维数组
    Centos7 GUI卸载安装gnome
    linux离线安装软件(三)——Centos7以源码编译方式安装两个版本gcc
    yum和源码编译安装nginx
    Linux修改移动硬盘文件类型
    Django部署时STATIC/MEDIA配置
  • 原文地址:https://www.cnblogs.com/mozheaishang/p/10088508.html
Copyright © 2020-2023  润新知