原题
Description
The look and say sequence is defined as follows. Start with any string of digits as the first element in the sequence. Each subsequent element is defined from the previous one by “verbally” describing the previous element. For example, the string 122344111 can be described as “one 1, two 2’s, one 3, two 4’s, three 1’s”. Therefore, the element that comes after 122344111 in the sequence is 1122132431. Similarly, the string 101 comes after 1111111111. Notice that it is generally not possible to uniquely identify the previous element of a particular element. For example, a string of 112213243 1’s also yields 1122132431 as the next element.
Input
The input consists of a number of cases. The first line gives the number of cases to follow. Each case consists of a line of up to 1000 digits.
Output
For each test case, print the string that follows the given string.理解&思路
用语言描述一段数字,比如“113355”可以描述为2个1,2个3,2个5。即212325。
我的思路就是用字符串承载这个1000位的”数字“,然后把字符串第一位赋值给一个容器,从字符串第二位开始,如果字符串值与容器值相等则计数器加一,接着判断下一位,如果字符串值与容器值不相等则输出计数器与容器值,同时计数器值变为1,容器值变为当前字符串值。AC代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string a,b;
int i,j,k,n,all;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a;
b[0]=a[0];
all=0;
for(j=0;j<a.size();j++)
{
if(a[j]==b[0])
{
all++;
if(j==a.size()-1)
cout<<all<<b[0];
}
else
{
cout<<all<<b[0];
b[0]=a[j];
all=1;
if(j==a.size()-1)
cout<<all<<b[0];
}
}
cout<<endl;
}
}
- 总结
第一遍没A,因为把B定义成了 int 型。
代码中‘容器’B,不能定义为整形int,因为要承载的数据类型为字符型。