The count-and-say sequence is the sequence of integers beginning as follows:
 1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
 11 is read off as "two 1s" or 21.
 21 is read off as "one 2, then one 1" or 1211.
 
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
Subscribe to see which companies asked this question
分析:
这题太凶残了,实在没能一下理解题目意思
别人的分析:每一个数字串,从最高位开始读起,对应每个数字的个数x个,其值为y;则记为xy;且每个数字串与其前一个数字串相关;比如111221就应该读成312211那么既然题意理解了,这个问题就简单了先将数字转换出字符,再将翻译(相邻的字符是否一样)成对应规则的字符串即可
class Solution {
	string numToString(int num)
	{
		string str = "";
		int carry = 0;
		while (num)
		{
			carry = num % 10;
			str += to_string(carry);
			num /= 10;
		}
		reverse(str.begin(),str.end());
		return str;
	}
public:
	string countAndSay(int n) {
		if (n == 0)
			return "";
		string str = numToString(n);//转换成相同顺序的字符串数字
		string ans = "";
		int i = 0;
		int curCount = 1;
		char curChar = str[i];
		i++;
		while (i<str.size())
		{
			if (str[i] == curChar)//一样
			{
				curCount++;
				i++;
			}
			else//不一样时就获取结果
			{
				ans += to_string(curCount) + curChar;
				curChar = str[i];
				curCount = 1;
				i++;
			}
		}
		ans += to_string(curCount) + curChar;//末尾字符串的追加
		return ans;
	}
};LeetCode出错证据:
明明题目中说了1应该读作“11”的,这里却读成了1
 
 
我的程序的运行结果:
 
 
完整的测试程序:
#include "vector"  
#include <iostream>  
#include "fstream"
#include "algorithm"  
#include <stdio.h>
#include "string"
#include <cmath>
#include <cstdlib>
#include <unordered_set>
using namespace std;
class Solution {
	string numToString(int num)
	{
		string str = "";
		int carry = 0;
		while (num)
		{
			carry = num % 10;
			str += to_string(carry);
			num /= 10;
		}
		reverse(str.begin(),str.end());
		return str;
	}
public:
	string countAndSay(int n) {
		if (n == 0)
			return "";
		string str = numToString(n);//转换成相同顺序的字符串数字
		string ans = "";
		int i = 0;
		int curCount = 1;
		char curChar = str[i];
		i++;
		while (i<str.size())
		{
			if (str[i] == curChar)//一样
			{
				curCount++;
				i++;
			}
			else//不一样时就获取结果
			{
				ans += to_string(curCount) + curChar;
				curChar = str[i];
				curCount = 1;
				i++;
			}
		}
		ans += to_string(curCount) + curChar;//末尾字符串的追加
		return ans;
	}
};
int main()
{
	int n = 221;
	Solution s;
	while (n != 0)
	{
		cin >> n;
		cout << s.countAndSay(n) << endl;
	}
	return 0;
}
 
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50460485
原作者博客:http://blog.csdn.net/ebowtang
 
                   
                   
                   
                   
                             
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1394
					1394
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            