在整数数组中查找缺少的数字(来自geeksforgeeks.com的问题) [英] Finding the missing number in an integer array (question from geeksforgeeks.com)

查看:67
本文介绍了在整数数组中查找缺少的数字(来自geeksforgeeks.com的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个整数数组,其中整数按递增顺序排序,相邻元素之间的差异是一,除了一对特殊的整数,其中差值为2。我必须找到丢失的整数。我已经编写了代码,到目前为止,我看不出任何问题,但是没有生成所需的输出。我想帮助你了解我的代码中的错误。



示例:11 12 13 15

缺少的数字是14


例如:3 5 6 7

缺失的整数是4



我尝试过:



这是我的代码。我将下面的数组值输出为12。

I am given an array of integers where the integers are sorted in an increasing order and the difference between adjacent elements is one except for a particular pair of integers where the difference is two. I have to locate the missing integer. I have written the code and so far, I cannot see any problem with it but the required output is not being produced. I would like yourhelp in understanding the mistake in my code.

Example: 11 12 13 15
The missing number is 14

Example: 3 5 6 7
The missing integer is 4

What I have tried:

This is my code. I am gettin the output as 12 for the array values below.

<pre>// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iso646.h>
#include <vector>
#include <string>
#include<iostream>
#include <algorithm>


using namespace std;

int FindMissingNumber(vector<int>& nums)
{
	int result = 0;

	//sort(nums.begin(), nums.end());

	for (int i = 0; i < nums.size(); i++)
	{
		for (int j = i + 1; j < nums.size() - 1; j++)
		{
			if (nums[j] - nums[i] == 2)
			{				
				result = (nums[i] + 1);
			}
		}
	}

	return result;
}

int main() {
 
	vector<int> nums = { 10, 11, 12, 13, 15 };

		cout << FindMissingNumber(nums) << endl;	

	system("pause");
	return 0;
}

推荐答案

FindMissingNumber 函数的逻辑是有缺陷的。尝试:

The logic of your FindMissingNumber function is flawed. Try:
#include <vector>
#include <string>
#include <iostream>


using namespace std;

bool FindMissingNumber(const vector<int>& nums, int & result)
{
  for (size_t i = 0; i < nums.size()-1; i++)
  {
    if ( nums[i] != nums[i+1]-1)
    {
      result = (nums[i]+1);
      return true;
    }
  }
  return false;
}

int main()
{
  vector<int> nums = { 10, 11, 12, 13, 15 };

  int result;
  if ( FindMissingNumber( nums, result) )
    cout << "missing number is " << result << endl;
  else
    cout << "there is no missing number" << endl;
  return 0;
}


引用:

我写的代码到目前为止,我看不出任何问题但是没有生成所需的输出。

I have written the code and so far, I cannot see any problem with it but the required output is not being produced.



正如已经告诉过的,使用调试器来查看你的代码在做什么。 />
想想你需要做些什么来手动解决问题,看看你的代码用调试器做了什么。



当你穿上不知道你的代码在做什么或为什么它做它做的事情,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具



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

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

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

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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


As already told you, use the debugger to see what your code is doing.
Think about what you have to do to solve the problem by hand, and look at what your code is doing with debugger.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger 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, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
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 find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于在整数数组中查找缺少的数字(来自geeksforgeeks.com的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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