高手进来解决个问题啊

yaoyin0819 2008-04-02 05:49:05
输入:111332233
输出:31232223
输入:1111111111
输出:101

意思就是:连续3个1连续2个3连续2个2连续2个3 输出结果就是31232223
连续10个1 输出结果就是101

哪位高手能解决吗?

...全文
109 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shore1111 2008-04-02
  • 打赏
  • 举报
回复
提示:既然用到统计字符个数,最好用容器里的count() 函数喽
学了C++,最好用新的东西~~
字符的操作最好用容器来处理~~
JYYCOM 2008-04-02
  • 打赏
  • 举报
回复
用堆栈应该也可以吧。
rushman 2008-04-02
  • 打赏
  • 举报
回复
#include <stdio.h>

void main(void){
char input[40];
char * pc;
char cc;
int count;

while(gets(input)){
if(0 == *input)break;
for(pc = input,cc = *input,count = 0;*pc != '\0';pc++){
if(cc != *pc){
printf("%d%c",count,cc);
cc = *pc;
count = 1;
continue;
}
count++;
}
printf("%d%c\n",count,cc);
}
}
劲草 2008-04-02
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
void main()
{
char ch[20];
int i=0,j=1;
gets(ch);


while(ch[i]!='\0')
{
if(ch[i]>='0'&&ch[i]<='9')
{
if(ch[i+1]==ch[i])
{
j++;i++;
}
else
{
cout<<j<<ch[i];
j=1;i++;
}

}

else
cout<<"您的输入有误!"<<endl;

}


}

popyy0101 2008-04-02
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
const int size = 100;
int main()
{
char s[size];
cin.getline(s,size);
int i = 0;
int count = 0;
int oldNum;
int num;
char c;
while(s[i] != 0){
c = s[i++];
num = atoi(&c);
if( i == 1 || num == oldNum){
count++;
}
else{
cout << count << oldNum;
count = 1;
}
oldNum = num;
}
cout << count << oldNum;
}
应该差不多吧
c_spark 2008-04-02
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>

#define MAXSIZE 100

int main()
{
int i,count;
char str[MAXSIZE];
while(scanf("%s",str) != EOF)
{
for(i = 1,count = 1; i < strlen(str); ++i)
{
if(str[i-1] == str[i])
{
++count;
continue;
}
printf("%d%c",count,str[i-1]);
count = 1;
}
printf("%d%c\n",count,str[i-1]);
}
return 0;
}


这个看看行不?
fallinleave 2008-04-02
  • 打赏
  • 举报
回复
如果你的初始输入是input,

int* input;
int* output;

int nLen;

//Get the input;
....

nLen = length of input;

output = new int[2*nLen];

for(int i=0, j=0; i<nLen-1; i++)
{
output[j+1] = input[i];
int count = 0;
while(input[i] == input[i+1])
{
i++;
count++;
}
output[j] = count + 1;
j = j + 2;
}
yaoyin0819 2008-04-02
  • 打赏
  • 举报
回复
字符串我知道啊 问题是怎么做? 我是新手~~
paidfighting 2008-04-02
  • 打赏
  • 举报
回复
用字符串就行了

64,664

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧