Home  >  Q&A  >  body text

在计蒜客oj用c++编写的一个简单的代码出现段错误

#include<iostream>
using namespace std;

int main(void)
{
    char a[50],b[50],ch;
    int i = 0,j = 0;
    cin.get(ch);
    while(ch != '\n')
    {
        a[i] = ch;
        i++;
        cin.get(ch);
    }
   // getchar();
    cin.get(ch);
    while(ch != '\n')
    {
        b[j] = ch;
        j++;
        cin.get(ch);
    }
   // getchar();
    
    for(int n = 0;n < i;n++)
    {
        if(a[n] == '1' && b[n] == '1')
        {
            cout << '1';
        }
        else if(a[n] == '1' && b[n] == '0')
        {
            cout << '0';
        }
        else if(a[n] == '0' && b[n] == '1')
        {
            cout << '0';
        }
        else
        {
            cout << '1';
        }
    }
    
    return 0;
}

样例输入没有错误,只是一直说是段错误,求大神看看

阿神阿神2714 days ago586

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 15:01:56

    It should be enough to change a[50] and b[50] to a[51] and b[51]

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 15:01:56

    In fact, if you use XOR judgment, you don’t need an array. You need to XOR first and then negate it.

    !(1 ^ 1) = 1
    !(1 ^ 0) = 0
    !(0 ^ 1) = 0
    !(0 ^ 0) = 1

    reply
    0
  • Cancelreply