在在线编译器上运行此程序时,我得到分段错误,有人可以告诉原因并纠正我的错误 [英] While running this program on an online compiler I am getting segmentation fault can someone tell why and correct my mistake

查看:119
本文介绍了在在线编译器上运行此程序时,我得到分段错误,有人可以告诉原因并纠正我的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是给出一个数字数组以及一些测试用例我试图以最大总和返回对的计数器因此我在第一行输入测试用例然后我将数组作为输入和排序后的数组我使用逻辑排序对的计数器,在排序的数组中如

8 7 7 5 4 3

将有两个最大值和我从索引1开始,只要下一个索引等于前一个索引,我就会增加计数器



我尝试过:



What I am trying to do is given an array of numbers along with some test cases I am trying to return the counter of pairs with maximum sum therfore I am taking input in first line the test case then i am taking the array as input and after sorting the array I am returning the counter of pairs using logic that in sorted array like
8 7 7 5 4 3
there will be two max sum and I am starting at index 1 and as long as the next index is equal to the previous one I am increasing the counter

What I have tried:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,counter=1,in=1;
        cin>>n;
        vector<int> arr;
        for(int i=0;i<arr.size();i++)
        {
            cin>>arr[i];
        }
        sort(arr.begin(),arr.end(),greater<int>());
        while(arr[in]==arr[in+1])
        {
            counter++;
            in++;
        }
       cout<<counter<<endl;
    }
}

推荐答案

在while循环中,您声明了一个整数向量。最初它是空的,因此它的大小在for循环中将为零,因此无论输入的值是什么,都不会输入任何内容。我猜它在数组内容的while循环比较中出错了。 for循环需要转到n,而不是arr.size()。
Inside the while loop you declare a vector of integers. Initially it is empty so its size will be zero in the for loop so nothing will be input into it, regardless of what the value of in is. I would guess that it faults in the while loop comparison of the array contents. The for loop needs to go to n, not arr.size().


问题是你的最内层循环不能正确计算对。

一次你已经对输入数组进行了排序,你的数据将如下所示:

The problem is that your innermost loop doesn't count pairs properly.
Once you have sorted the array of inputs, your data will look like this:
3 4 5 7 7 8

所以当你尝试访问时数据,你的循环做的第一件事就是将第二个元素与第三个元素进行比较:

So when you try to access the data, the first thing your loop does is compare the second element with the third:

int n,counter=1,in=1;
...
while(arr[in]==arr[in+1])




while(arr[1]==arr[1+1])




while(4==5)

这显然是false,所以永远不会输入你的循环。你永远不会看第一个元素,所以如果你的数据是

Which is obviously false, so your loop is never entered. And you never look at the first element at all, so if your data is

1 1 2 3 4

你将永远找不到匹配 - 这意味着你的循环将永远不会退出,或者会给你一个非法的访问错误和崩溃。



如果你想计算对,你需要使用一个for循环,从零开始结束(项目数 - 1)并计算它arr [i]和arr [i + 1]是相同的。



在纸上试试,你我会明白我的意思。

you will never find a match at all - and that means your loop will never exit, or will give you an illegal access error and crash.

If you want to count pairs, you need to use a for loop, starting at zero and ending an (number of items - 1) and counting it arr[i] and arr[i + 1] are the same.

Try it on paper, and you'll see what I mean.


Quote:

在在线编译器上运行这个程序的时候我是获得分段错误

While running this program on an online compiler I am getting segmentation fault



在学习的过程中,我建议在您的计算机上使用编译器,因为它附带工具,有趣的是调试器。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器步骤上运行代码逐步检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有魔法在调试器中,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你显示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


As you are learning, I recommend to use a compiler on your computer, because it comes with tools, the interesting one is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于在在线编译器上运行此程序时,我得到分段错误,有人可以告诉原因并纠正我的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆