码迷,mamicode.com
首页 > 其他好文 > 详细

华为机考--字符串压缩

时间:2014-09-09 17:45:49      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   2014   

通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。 

压缩规则: 

1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc". 

2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz" 

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     char s[50];
 6     int sCount;
 7     int i;
 8     while(cin>>s)
 9     {
10         sCount=1;
11         for (i=0;s[i];i++)
12         {
13             if (s[i]==s[i+1])
14             {
15                 sCount++;
16             } 
17             else
18             {
19                 if (sCount==1)
20                 {
21                     cout<<s[i];
22                 } 
23                 else
24                 {
25                     cout<<sCount<<s[i];
26                 }
27                 sCount=1;
28             }
29         }
30         cout<<endl;
31     }
32     return 0;
33 }

运行结果:

bubuko.com,布布扣

华为机考--字符串压缩

标签:style   blog   http   color   os   io   ar   for   2014   

原文地址:http://www.cnblogs.com/LiuYujie/p/3962725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!