在计蒜客oj用c++编写的一个简单的代码出现段错误
阿神
阿神 2017-04-17 14:59:56
0
2
619

#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;
}

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

阿神
阿神

闭关修行中......

reply all(2)
PHPzhong

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

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!