python 字母顺序计数_计数并说出顺序

python 字母顺序计数

Problem statement:

问题陈述:

The count-and-say sequence is the sequence of integers with the first five terms as following:

计数序列是具有前五个项的整数序列,如下所示:

  1. 1

    1个

  2. 11

    11

  3. 21

    21

  4. 1211

    1211

  5. 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:

算法:

  1. Check for base cases

    检查基本情况

  2.     if(n==0)
            return "";
        if(n==1)
            return "1";
    
    
  3. 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”初始化结果 。 结果是将为每个标签打印的输出字符串。

  4. Print result for level 1. (As level >1 now)

    打印级别1的结果 。(现在级别> 1)

  5. Declare a temporary string only to get the current level result. Let the temporary string be temp.

    声明一个临时字符串只是为了获得当前级别的结果。 让临时字符串为temp 。

  6. For i=1:n //iterate for rest of the rows

    对于i = 1:n //重复其余行

    1. Store the length of result (carrying the result of last processed level actually) string length
    2. 存储结果的长度(实际携带最后处理的水平的结果)字符串的长度
    3. Let the length be len
    4. 让长度为len
    5. For j=0:lentemp=temp+ to_string(count) + result[j] //count comes first
    6. 对于j = 0:len temp = temp + to_string(count)+ result [j] //首先计数
    7. Current level string is processed in temp, So update result=temp
    8. 当前级别的字符串在temp中处理,因此update result = temp
    9. Print result
    10. 打印结果
    11. Clear temp for further levels
    12. 清除温度以达到更高的水平
  7. End For

    结束于

C++ implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;

void countAndSay(int n) {
	//base cases
	if(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 result
	for(int i=1;i<n;i++){ //iterate for n-1 rows
		//iterate upto current string length
		int len=result.length();
		for(int j=0;j<len;j++){
			int count=1;//initialize count as 1
			//find count for repeated number
			while(j+1<len && result[j]==result[j+1] ){
				count++;
				j++;
			}
			//convert the count to string and add to 
			//temprary result, then add original no
			temp+=to_string(count)+result[j];
		}
		//assign temporary result to original result 
		//& print for current level 
		result=temp;
		cout<<result<<endl;
		//clear the temporary result
		temp="";
	}
}

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 sequence
	coutAndSay(n);
	
	return 0;
}


Output

输出量

First run:
count and Say problem.....
enter n, no of rows
3
Printing Count and Say sequence...
1
11
21


Second run:
enter n, no of rows
6
Printing Count and Say sequence...
1
11
21
1211
111221
312211

Third 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
13211311123113112211


How 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 字母顺序计数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值