python 字母顺序计数_计数并说出顺序
python 字母順序計數
Problem statement:
問題陳述:
The count-and-say sequence is the sequence of integers with the first five terms as following:
計數序列是具有前五個項的整數序列,如下所示:
1
1個
11
11
21
21
1211
1211
111221
111221
1 is read off as "one 1" or 11.
1被讀出為“一個1”或11。
11 is read off as "two 1s" or 21.
11被讀出為“兩個1”或21。
21 is read off as "one 2, then one 1" or 1211.
21被讀出為“一個2,然后一個1”或1211。
That means every integer (repeated continuously) is read off with its count value.
這意味著將讀取每個整數(連續重復)及其計數值。
For a given n, Print the count and say sequence.
對于給定的n ,打印計數并說出順序。
Examples
例子
For n=0 It's a blank string -------------------------For n=1 Its "1" -------------------------For n=2, we need to evaluate the previous string which is for n=1 For n=1 "1" So one "1" Thus For n=2 "11" -------------------------Needless to say for n=3 Evaluating n=2 results in two "1"->"21" So on...Solution:
解:
Pre-requisite:
先決條件:
to_string function: In C++ we have an in-build function which converts a numerical value to its string representation. Like 1 is converted to "1" (the first 1 is of int datatype, while the second is of string datatype).
to_string函數:在C ++中,我們有一個內置函數,該函數將數字值轉換為其字符串表示形式。 像1轉換為“ 1”(第一個1是int數據類型,而第二個是字符串數據類型)。
prototype of the function is:
該函數的原型是:
string to_string (int value);Algorithm:
算法:
Check for base cases
檢查基本情況
For rest of the cases, it must start from 1, thus initialize result with string "1". result is our output string which will be printed for each label.
在其他情況下,它必須從1開始,因此用字符串“ 1”初始化結果 。 結果是將為每個標簽打印的輸出字符串。
Print result for level 1. (As level >1 now)
打印級別1的結果 。(現在級別> 1)
Declare a temporary string only to get the current level result. Let the temporary string be temp.
聲明一個臨時字符串只是為了獲得當前級別的結果。 讓臨時字符串為temp 。
For i=1:n //iterate for rest of the rows
對于i = 1:n //重復其余行
End For
結束于
C++ implementation
C ++實現
#include <bits/stdc++.h> using namespace std;void countAndSay(int n) {//base casesif(n==0)return "";if(n==1)return "1";//for rest of the cases, it must start from 1 //initialize result with string "1"string result="1";cout<<result<<endl;string temp;//temporary string to hold levelwise resultfor(int i=1;i<n;i++){ //iterate for n-1 rows//iterate upto current string lengthint len=result.length();for(int j=0;j<len;j++){int count=1;//initialize count as 1//find count for repeated numberwhile(j+1<len && result[j]==result[j+1] ){count++;j++;}//convert the count to string and add to //temprary result, then add original notemp+=to_string(count)+result[j];}//assign temporary result to original result //& print for current level result=temp;cout<<result<<endl;//clear the temporary resulttemp="";} }int main(){int n;cout<<"count and Say problem.....\n";cout<<"enter n, no of rows\n";cin>>n;//function to print count and say sequencecoutAndSay(n);return 0; } .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Output
輸出量
First run: count and Say problem..... enter n, no of rows 3 Printing Count and Say sequence... 1 11 21Second run: enter n, no of rows 6 Printing Count and Say sequence... 1 11 21 1211 111221 312211Third run: count and Say problem..... enter n, no of rows 10 Printing Count and Say sequence... 1 11 21 1211 111221 312211 13112221 1113213211 31131211131221 13211311123113112211How the program works?
該程序如何工作?
Let's solve an example for n=3 Base case not matched Result="1" Thus it prints "1" and proceeds for rest of two level Output at level-1 1 --------------------------------------------------------1st iteration In the outer for loop i=1 & i< 3 Length of result=1 (result="1") Inner loop executes only once, since len=1 Thus count=1 temp =to_string(1) +"1" ="1"+"1" ="11" result="11" temp="" output at level-2 11 So output printed up to now: 1 11 --------------------------------------------------------2nd iteration In the outer for loop i=2& i< 3 Length of result=2 (result="11") Inner loop executes twice, since len=2 Thus count=2 // "1" repeating twice, no other character temp =""+to_string(2) +"1" ("" is temp previously updated, cleared basically) ="2"+"1" ="21" result="21" temp="" output at level-3 21 So output printed up to now: 1 11 21 --------------------------------------------------------3rd iteration i=3 thus loop ends Output is printed Thus count and say sequence for n=3 1 11 21翻譯自: https://www.includehelp.com/icp/count-and-say-sequence.aspx
python 字母順序計數
總結
以上是生活随笔為你收集整理的python 字母顺序计数_计数并说出顺序的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 图形学 射线相交算法_计算机图形学中的阴
 - 下一篇: 数据库数据规范化看不懂_数据库管理系统中