题目
解决代码及点评
/* 字符串原地压缩 例如"aaabnndccdeee"压缩成"a3bn2dc2de3"*/#includeusing namespace std;void Condense(char *pszBuf1){ int nLen = strlen(pszBuf1); int nCount = 0; int i, j; for ( i = 0; i < nLen - 1; i++) { nCount = 1; while (pszBuf1[i] == pszBuf1[i+1]) { pszBuf1[i] = '0'; i++; nCount++; } if (nCount > 1) { pszBuf1[i-1] = pszBuf1[i]; pszBuf1[i] = nCount + '0'; } } for ( i = 0, j = 0; i < nLen && j < nLen;) { while (pszBuf1[i] != '0' && i < nLen) { i++; } while (pszBuf1[j] == '0' && j < nLen) { j++; } swap(pszBuf1[i], pszBuf1[j]); }}int main(){ char szBuf[] = "aaabnndccdeee"; Condense(szBuf); cout< <
代码下载及其运行
代码下载地址:http://download.csdn.net/detail/yincheng01/6704519
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行
程序运行结果