找出01数组中的0&&1连续的最大次数

issac_gang 2017-06-15 02:45:42
如题,程序不报错,正常运行,但就是没结果。是没结果,黑屏。求解答

#include <iostream>
void calculate0and1();
using namespace std;

void calculate0and1( char *str,int *max0,int *max1)
{
int temp0 = 0;
int temp1 = 0;
while (*str)
{
if (*str ==' 0')
{
(*max0)++;
if (*(++str) == '1')
{
if (temp0 < *max0)
{
temp0 = *max0;
}
*max0 = 0;
}
}
else if (*str == '1')
{
(*max1)++;
if (*(++str) == '0')
{
if (temp1 < *max1)
{
temp1 = *max1;
}
*max1 = 0;
}
}
}
*max0 = temp0;
*max1 = temp1;
}
int main()
{
char string[] = "00001111001010101010101010001011101001010";
int max0 = 0;
int max1 = 0;
calculate0and1(string, &max0, &max1);
cout << max0 << max1 << endl;
return 0;
}
...全文
303 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-06-15
  • 打赏
  • 举报
回复
人民的眼睛是雪亮的, 程序员的眼睛是贼亮的!
paschen 版主 2017-06-15
  • 打赏
  • 举报
回复
if (*str == ' 0') 改成: if (*str == '0') 多了一个空格 此外,建议单步跟踪程序运行,以分析观察原因
wallesyoyo 2017-06-15
  • 打赏
  • 举报
回复
引用 2 楼 u011779875 的回复:
没结果 当然就是死循环了 第一 if else if 你那else呢 因为字符串的最后的\0 不是'0'也不是'1' 第二 凡是写代码使用i++ ,++i的人 都是喜欢把问题复杂化的人 新生完全可以不使用它 除了在理论意义上++i的执行速度快一点而已 没什么大用 画蛇添足的人 才喜欢这么使用 前提是你得教学费 走很多弯路 学一个只能在C++Party上 装作一个大神级别的程序员而已、 第三 这句话 出的问题 if (*(++str) == '0') 没事别学人家用++i 写++i的人 总会有一不小心 走在未定义行为的高速列车上。
 while (*str)
    {
        if (*str ==' 0')
        {
            (*max0)++;
            if (*(++str) == '1')
            {
                if (temp0 < *max0)
                {
                    temp0 = *max0;                               
                }
                *max0 = 0;
            }
        }
        else if (*str == '1')
        {
            (*max1)++;
            if (*(++str) == '0')
            {
                if (temp1 < *max1)
                {
                    temp1 = *max1;
                }
                *max1 = 0;
            }
        }
    }
人家只需要判断0和1,不需要判断别的字符啊,不写else没毛病,字符串结尾已经在while循环判断了。
忘世麒麟 2017-06-15
  • 打赏
  • 举报
回复
上一楼的count变量是没有意义的.这是C的写法:
#include <stdio.h>
void calculate0and1( char *str,int *max0,int *max1)
{
    int temp0 = 0;
    int temp1 = 0;
    while (*str)
    {
        temp0 = 0;
        temp1 = 0;
        while(*str && (*str=='1'))
        {
            temp1++;
            str++;
        }
        *max1 = temp1>*max1?temp1:*max1;
        while(*str && (*str=='0'))
        {
            temp0++;
            str++;
        }
        *max0 =temp0>*max0?temp0:*max0;
    }
}
int main()
{
    char string[] = "00001111001010101010101010001011101001010";
    int max0 = 0;
    int max1 = 0;
    calculate0and1(string, &max0, &max1);
    printf("%d\n",max0);
    printf("%d\n",max1);
    return 0;
}
希望楼主学语言的时候学到精髓,下意识的运用一门语言的特点去解决问题.
忘世麒麟 2017-06-15
  • 打赏
  • 举报
回复
C++的程序,但是除了输入输出和头文件外,没看到C++的特性.
void calc0And1(string str,int& max0,int& max1 )
{
    int count = 0;
    int tmp0 = max0;
    int tmp1 = max1;
    string::iterator pos = str.begin();
    for (string::iterator it = str.begin();it != str.end();)
    {
        if (*it == '1')
        {
            while (it != str.end() && *it == '1')
            {
                it++;
                count++;
            }
            tmp1 = it-pos;
            pos = it;
        }
        else
        {
            while(it != str.end() && *it == '0')
            {
                it++;
                count++;
            }
            tmp0 = it-pos;
            pos = it;
        }
        max0 = max0<tmp0?tmp0:max0;
        max1 = max1<tmp1?tmp1:max1;
    }
}
int main()
{
    string str("00001111001010101010101010001011101001010");
    int max0 = 0;
    int max1 = 0;
    calc0And1(str, max0, max1);
    cout << max0 <<endl << max1 << endl;
    return 0;
}
做或不做 2017-06-15
  • 打赏
  • 举报
回复
没结果 当然就是死循环了 第一 if else if 你那else呢 因为字符串的最后的\0 不是'0'也不是'1' 第二 凡是写代码使用i++ ,++i的人 都是喜欢把问题复杂化的人 新生完全可以不使用它 除了在理论意义上++i的执行速度快一点而已 没什么大用 画蛇添足的人 才喜欢这么使用 前提是你得教学费 走很多弯路 学一个只能在C++Party上 装作一个大神级别的程序员而已、 第三 这句话 出的问题 if (*(++str) == '0') 没事别学人家用++i 写++i的人 总会有一不小心 走在未定义行为的高速列车上。
 while (*str)
    {
        if (*str ==' 0')
        {
            (*max0)++;
            if (*(++str) == '1')
            {
                if (temp0 < *max0)
                {
                    temp0 = *max0;                               
                }
                *max0 = 0;
            }
        }
        else if (*str == '1')
        {
            (*max1)++;
            if (*(++str) == '0')
            {
                if (temp1 < *max1)
                {
                    temp1 = *max1;
                }
                *max1 = 0;
            }
        }
    }
wallesyoyo 2017-06-15
  • 打赏
  • 举报
回复
if (*str ==' 0') 你的 ' 0' 多了一个空格。。。

64,439

社区成员

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

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