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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540712.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信网页扫码登录的实现

为了让用户登录网站的门槛更低&#xff0c;微信扫一扫登录变得越来越广泛&#xff0c;所以最近加紧赶制的项目中有用到这个功能&#xff0c;此篇文字的出发点基于微信开放平台已经配置好域名&#xff08;80端口&#xff09;并且认证成功获得app_id和secret并有权限调用微信的接…

希尔密码_希尔密码| 网络安全

希尔密码Now, Hill Cipher is a very basic cryptographic technique which is used to convert a string into ciphertext. This technique was invented by an American Mathematician "Lester Sanders Hill". This is a polygraphic substitution cipher because …

Android 那些年,处理getActivity()为null的日子

在日常开发中的时候&#xff0c;我们经常会使用ViewPagerFragment进行视图滑动&#xff0c;在某些部分逻辑也许我们需要利用上下文Context&#xff08;例如基本的Toast&#xff09;&#xff0c;但是由于Fragment只是衣服在Activity容器的一个试图&#xff0c;如果需要拿到当前的…

设计模式状态模式uml_UML的完整形式是什么?

设计模式状态模式umlUML&#xff1a;统一建模语言 (UML: Unified Modeling Language) UML is an abbreviation of Unified Modeling Language. In the field of software engineering, it is a visual modeling language that is standard in quality. It makes it available t…

idea debug快捷键

idea的debug调试快捷键 F9 resume programe 恢复程序 AltF10 show execution point 显示执行断点 F8 Step Over 相当于eclipse的f6 跳到下一步 F7 Step Into 相当于eclipse的f5就是 进入到代码 AltshiftF7 Force Step Into 这个…

vqa mcb_MCB的完整形式是什么?

vqa mcbMCB&#xff1a;微型断路器 (MCB: Miniature Circuit Breaker) MCB is an abbreviation of "Miniature Circuit Breaker". MCB是“微型断路器”的缩写 。 It is an automatically operated electronics switch. It is designed to detect the fault in the e…

返回表达式列表中最小值least(exp1,exp2,exp3,……,expn)

1 least(exp1,exp2,exp3,……,expn)2 【功能】返回表达式列表中值最小的一个。如果表达式类型不同&#xff0c;会隐含转换为第一个表达式类型。3 【参数】exp1……n&#xff0c;各类型表达式4 【返回】exp1类型5 6 【示例】7 SELECT least(10,32,123,2006) FROM dual;8 9 SEL…

Java Short类hashCode()方法及示例

短类hashCode()方法 (Short class hashCode() method) hashCode() method is available in java.lang package. hashCode()方法在java.lang包中可用。 hashCode() method is used to return hashcode of the Short object.hashCode()方法用于返回Short对象的哈希码。 hashCode(…

CentOS忘记普通用户密码解决办法

普通用户忘记密码 1.使用root用户登录系统&#xff0c;找到/etc/shadow文件。 2.找到用户名开头的那一行&#xff0c;例如我的用户名为pds,&#xff0c;以冒号为分割符&#xff0c;红色部分是密码加密部分 pds:$1$CivopRgF$ajWQ54W1XJbifFjm05Jk/1:15353:0:99999:7::: 3.pds是我…

julia 编程语言_Julia编程语言中的变量

julia 编程语言Julia中的变量 (Variables in Julia) Just like other programming languages, in Julia variables are the name of memory blocks that are associated (or bound) to a value. It is useful when a value to be stored or to be accessed in/from memory loca…

php脚本超时 结束执行代码

函数&#xff1a;stream_context_create ,file_get_content 创建并返回一个文本数据流并应用各种选项&#xff0c;可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。函数原型&#xff1a;resource stream_context_create ([ a…

c#byte字节流的读取_C#中的byte关键字

c#byte字节流的读取C&#xff03;字节关键字 (C# byte keyword) In C#, byte is a keyword which is used to declare a variable that can store an unsigned value between 0 to 255. byte keyword is an alias of System.Byte. 在C&#xff03;中&#xff0c; byte是一个关键…

esp32的GPIO操作

对于任何一款芯片&#xff0c;GPIO接口是其最基本的组成部分&#xff0c;也是一款芯片入门的最基本操作&#xff0c;下面论述下 关于esp32开发版的GPIO操作&#xff0c;本文中重点讲解下 关于如何创建eclipse工程&#xff0c;并通过eclipse下载到esp32中去&#xff08;本文的工…

c# bool?和bool_C#中的bool关键字

c# bool?和boolC&#xff03;bool关键字 (C# bool keyword) In C#, bool is a keyword which is used to declare a variable that can store Boolean values true or false. bool keyword is an alias of System.Boolean. 在C&#xff03;中&#xff0c; bool是一个关键字&am…

聚焦数据的力量——全球领先安全技术分享会在京召开

ZD至顶网安全频道 04月21日 综合消息&#xff1a; 由中国网络安全与信息化产业联盟、360共同主办的“数据的力量——全球领先安全技术分享会“今日在北京成功召开。来自政府、企业、教育、投资机构和产业联盟的300多位嘉宾参加了本次技术分享会&#xff0c;共同就安全产业发展趋…

algol语言_ALGOL的完整形式是什么?

algol语言ALGOL&#xff1a;算法语言 (ALGOL: Algorithmic Language) ALGOL is an abbreviation of "Algorithmic Language". ALGOL是“算法语言”的缩写 。 It is a family of very significant computer programming languages, initially designed and created i…

Qt/QML编程学习之心得:一个.qml文件调用另一个.qml文件(十七)

在c++中,一个文件调用另外一个文件最直接最快捷的方式就是#incldue<头文件>的使用,那么在元数据描述性语言QML中,如何从一个界面描述调用另外一个界面描述,一个.qml文件调用另外一个.qml呢?QML虽然有个import,但是用法可以说完全不同于#include。 引用方法1:直接…

如何设置Fedora默认从命令行启动?

2019独角兽企业重金招聘Python工程师标准>>> Sumary:因为在Fedora中没有/etc/initab文件我们不方便从这里设置它的runlevel target&#xff0c;但是Linux又给我们提供了一个强悍的工具systemd,我们可以用system来链接默认的启动级别&#xff0c;所以开始吧&#xff…

scala 线性回归_Scala的特征线性化

scala 线性回归Scala | 特性线性化 (Scala | Trait Linearization) In Scala programming language, trait linearization is a property that helps to rectify ambiguity when instances of a class that are defined using multiple inheritances from different classes an…

MDK C++中对内联的极度优化

先来看看我们SmartIRQ的具体实现 // 智能IRQ&#xff0c;初始化时备份&#xff0c;销毁时还原 class SmartIRQ { public:force_inline SmartIRQ(bool enable false){_state __get_PRIMASK();if(enable)__enable_irq();else__disable_irq();}force_inline ~SmartIRQ(){__set_P…