1. 程式人生 > >1140 Look-and-say Sequence(20 分)

1140 Look-and-say Sequence(20 分)

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

思路:

考試的時候卡這道題卡了很久,終究是沒讀懂題...

第n+1個串是第n個串的描述。   比如第n個串是11122234(隨便寫的),那麼第n+1個串就是13233141,這個意思就是在第n個串中  1有3個, 2有3個, 3有1個, 4有1個, 所以是13233141。 

C++:

#include "cstdio"
#include "iostream"
#include "string"
using namespace std;
int main(){
	int n,k;
	string s,ans;
	cin>>s>>n;
	for (int i=1;i<n;i++)
	{
		string t;
		for (int j=0;j<s.length();j=k)
		{
			for (k=j;k<s.length()&&s[k]==s[j];k++);
			t+=to_string(s[j]-'0')+to_string(k-j);
		}
		s=t;
	}
	cout<<s<<endl;
	return 0;
}

相關推薦

pta 1140 Look-and-say Sequence 20

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. T

1140 Look-and-say Sequence 20 (cj)

1140 Look-and-say Sequence (20 分) Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D11221311

【笨方法學PAT】1140 Look-and-say Sequence 20

一、題目 Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213111, ... where D is in [0,

1140 Look-and-say Sequence20

Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213111, ... where D is in [0, 9] excep

1140 Look-and-say Sequence20

//將每個不同的字元存入,初始字元後面的個數為1, //之後下一個時判斷是否去前一個字元相同,相同則個數++; //不同的將新字元存入,並且個數初始為1。 #include <cstdio> #include <cstring> int main() { char s

1140 Look-and-say Sequence (20)

-s gin win div CI clu TP include pan Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D1122

PAT 1140 Look-and-say Sequence [比較]

1140 Look-and-say Sequence (20 分) Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D

1065 A+B and C (64bit) 20

1065 A+B and C (64bit) (20 分) 程式碼 c++版 #include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, sum; int n; ci

1065 A+B and C (64bit) 20 溢位判定

題目 Given three integers A, B and C in [ −

Data Structures and Algorithms (English) - 6-14 Count Connected Components20

題目連結:點選開啟連結   題目大意:略。   解題思路:略。   AC 程式碼 int vis[MaxVertexNum]; void dfs(LGraph Graph, int v) { vis[v]=1; PtrT

PAT 1065 A+B and C 20 Java實現

題意: 計算A+B是否大於C 思路: 用Java做很容易,可以直接用大數類,就不用模擬了~~ 程式碼: package adv1065; import java.io.BufferedReader; import java.io.IOException; impor

A1140 Look-and-say Sequence

用string操作很方便,沒什麼坑點,看懂題意就行了。主要就是統計連續的一片區域有幾個連續的數字。 #include <stdio.h> #include <string> #include <iostream> using namespace std

6-8 Percolate Up and Down 20

Write the routines to do a "percolate up" and a "percolate down" in a binary min-heap. Format of functions: void PercolateUp( int p, PriorityQueue

習題3.8 符號配對20 浙大版《數據結構第2版》題目集

檢查 size pro 是否 ring 所有 編寫 bre ace 請編寫程序檢查C語言源程序中下列符號是否配對:/*與*/、(與)、[與]、{與}。 輸入格式: 輸入為一個C語言源程序。當讀到某一行中只有一個句點.和一個回車的時候,標誌著輸入結束。程序中需要檢

習題3.4 最長連續遞增子序列20 浙大版《數據結構第2版》題目集

space align font list 格式 ott mar 不能 第一次 給定一個順序存儲的線性表,請設計一個算法查找該線性表中最長的連續遞增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最長的遞增子序列為(3,4,6,8)。 輸入格式: 輸入

習題3.9 堆棧操作合法性20 浙大版《數據結構第2版》題目集

ram xxxxx text -html base logs main 格式 using 假設以S和X分別表示入棧和出棧操作。如果根據一個僅由S和X構成的序列,對一個空堆棧進行操作,相應操作均可行(如沒有出現刪除時棧空)且最後狀態也是棧空,則稱該序列是合法的堆棧操作

習題2.2 數組循環左移20 浙大版《數據結構第2版》題目集

problem [] 要求 top out 數據結構 允許 cal right 本題要求實現一個對數組進行循環左移的簡單函數:一個數組a中存有n(>0)個整數,在不允許使用另外數組的前提下,將每個整數循環向左移m(≥0)個位置,即將a中的數據由(a?0?

習題2.8 輸出全排列20 浙大版《數據結構第2版》題目集

text ble 存在 base scripts html 數據 ext 運行時 請編寫程序輸出前n個正整數的全排列(n<10),並通過9個測試用例(即n從1到9)觀察n逐步增大時程序的運行時間。 輸入格式: 輸入給出正整數n(<10)。 輸出格

7-15 兩個有序鏈表序列的合並20

一行 htm text all color div 兩個 turn nbsp 已知兩個非降序鏈表序列S1與S2,設計函數構造出S1與S2的並集新非降序鏈表S3。 輸入格式: 輸入分兩行,分別在每行給出由若幹個正整數構成的非降序序列,用−1表示序列的結

7-16 一元多項式求導20 (有關while(scanf("%d",&n)!=EOF))

margin 常數 解題思路 return 做的 gin 遇到 bsp title 7-16 一元多項式求導(20 分) 設計函數求一元多項式的導數。 輸入格式: 以指數遞降方式輸入多項式非零項系數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。