查找数组中的寂寞整数 [英] Find the lonely integer in an array

查看:368
本文介绍了查找数组中的寂寞整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅如果你能这样的hackerrank挑战。

问题是找到一个数组寂寞整数,给定一个数组仅包含对除了一个孤独的整数。

问题是与这个测试用例

  9
4 9 95 93 57 57 4 9 93

9数组大小及以下的阵列

见突出code部分// ------

如果我把scanf函数(%d个,&安培; N)以上INT ARR [N] code工作正常,但轮给人可怕结果的其他方式。请帮我出

 的#include<&stdio.h中GT;INT lonely_integer为(int *一,INT大小);诠释主(){
    // n为数组大小,我是反变
    INT N,I,结果;
    // ---------------------
    INT ARR [N];
    scanf函数(%d个,&安培; N);
    // ---------------------
    的printf(%d个\\ N,N);
    对于(i = 0; I< N;我++){
        scanf函数(%d个,&安培;常用3 [I]);
    }
    结果= lonely_integer(ARR,N);
    的printf(%d个,结果);
    返回0;
}
INT lonely_integer为(int *一,诠释大小){
    INT I;
    INT RES = 0;
    对于(i = 0; I<大小;我++){
        RES =资源^ A [];
    }    返回水库;
}


解决方案

您想使用:

 的#include<&stdlib.h中GT;
/ * ... * /
INT * ARR;
scanf函数(%d个,&安培; N);
ARR =的malloc(sizeof的(INT)* N);

这样,改编被在运行时动态分配的,因此可以根据输入任意大小的 N

您最初在做什么(即宣告改编[N] recieving后 N 通过scanf函数: scanf函数(%d个,&安培; N); INT ARR [N]; )不是一个好主意,因为它使用的变长数组的,C的一项功能,是不是在最新的C标准是强制性的。

您看,改编在编译时被创建,你的正常的只能用恒定的前pression已知的初始化在编译时,它 N ,收到的用户输入,显然是一个变量不是。变长数组是基本上可以让你绕过这个规则的语言的一个特征,即,它们使你能够初始化数组在编译时不知道的长度。这已被standartized C99 ,但被列为可选为 C11

你做了什么的( INT ARR [N]; scanf函数(%d个,&安培; N); )之后是相当不合逻辑的,因为,你声明改编 N 整数数组的的您收到<$ C $的价值ç> N 作为用户输入,并且,好了,知道它的价值。打印不需要的,因为 N 最初初始化为未指定垃圾价值,这是你的VLA的尺寸变得什么,当你把它声明:

  INT ARR [N]; // n是垃圾在这一点上,你不知道大的改编怎么会!
scanf函数(%d个,&安培; N); //你得到你所需要的N值,但为时已晚,唉!

Please refer to this hackerrank challenge if you can.

The problem is to find the lonely integer in an array, given an array consists of only pairs except one lonely integer.

The problem is with this test case

9
4 9 95 93 57 4 57 93 9

9 is array size and below is the array

See the part of code highlighted by //------

If I place scanf("%d", &n) above int arr[n] code works fine, but gives horrible results the other way round. Please help me out

#include <stdio.h>

int lonely_integer(int* a, int size);

int main(){
    //n is size of array, i is counter variable
    int n, i, result;
    // ---------------------
    int arr[n];
    scanf("%d", &n);
    // ---------------------
    printf("%d\n", n);
    for(i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    result = lonely_integer(arr, n);
    printf("%d", result);
    return 0;
}


int lonely_integer(int* a, int size){
    int i;
    int res = 0;
    for(i = 0; i < size; i++){
        res = res ^ a[i];
    }

    return res;
}

解决方案

You'd want to use:

#include <stdlib.h>
/* ... */
int *arr;
scanf("%d", &n);
arr = malloc(sizeof(int) * n);

This way, arr gets dynamically allocated at runtime, so it can be of any size depending on the input n.

What you were originally doing (i.e. declaring arr[n] after recieving n via scanf: scanf("%d", &n); int arr[n];) is not a good idea because it makes use of Variable-Length Arrays, a feature of C that is not mandatory in the latest C standard.

You see, arr gets created at compile-time, and you normally can only initialize it with a constant expression known at compile-time, which n, a variable recieved as user input, obviously isn't. Variable-length arrays are a feature of the language that basically allows you to bypass this rule, i.e. they make you able to initialize an array to a length not known at compile-time. This has been standartized in C99, but was listed as "optional" as of C11.

What you did after that (int arr[n]; scanf("%d", &n);) is quite illogical because, well, you declare arr as an array of n integers before you recieve the value of n as user input, and, well, know its value. It prints garbage because n is originally initialized to an unspecified "garbage" value, and this is what your VLA's size becomes when you declare it:

int arr[n]; //n is garbage at this point, you have no idea how large arr will be!
scanf("%d", &n); //you got the value of n that you needed, but too late, alas!

这篇关于查找数组中的寂寞整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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