填充随机数的数组从C或C 1到10 ^ 10 ++ [英] Filling an array with random numbers from 1 to 10^10 in C or C++

查看:145
本文介绍了填充随机数的数组从C或C 1到10 ^ 10 ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

矿的分配的一部分是基于阵列(其大小由用户给出),其中包含随机数从1到10 ^ 10上。然后我们必须找到阵列的第k个较小的数字。下面是我的尝试:

 的#include< cstdlib>
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&iostream的GT;
#包括LT&;&time.h中GT;使用命名空间std;无效掉期(INT * X,诠释* Y)
{
    INT温度;
    TEMP = * X;
    * X = * Y;
    * Y =温度;
}INT choose_pivot(INT I,诠释J)
{
    返回((I + J)/ 2);
}//打印阵列
无效printarr(INT ARR [],INT N)
{
    INT I;
    对于(i = 0; I< N;我++)
        的printf(%d个\\ t的,编曲[I]);
}//查找算法
INT FIND1(INT ARR [],诠释左,右INT,INT K)
{
    INT I,J,透视;
    如果(左==右)
        返回ARR [左]
    其他
    {
        我离开=;
        J =右+ 1;
        支点= ARR [左]
        做
        {
            做{
                I = I + 1;
            }而(ARR [I]> =支点);
            做{
                当J = J-1;
            }而(ARR [J]< =支点);
            如果(ⅰ&所述; j)条
                掉期(ARR [I],编曲[J]);
        }而(J< = 1);
    }
    掉期(ARR [左],编曲[J]);
    如果(K == j)条
        返回ARR [J]。
    否则,如果(K< j)条
        FIND1(ARR,左,J-1,K);
    其他
        FIND1(ARR,J + 1,右,K-J);
}INT主(INT ARGC,CHAR *的argv [])
{
    函数srand(时间(NULL));
    诠释N,I,音响,K;
    的printf(给数组的大小:\\ n);
    scanf函数(%d个,&安培; N);
    INT引脚[N];
    对于(i = 0; I< N;我++)
        针[I] =((RAND()* RAND())%1000000000)+1;
    的printf(给K:\\ n);
    scanf函数(%d个,&安培; K);
    的printf(该数组包含以下号码:\\ n \\ n);
    printarr(针,N);
    音响= FIND1(销,0,N-1,K); //找到阵列中的第k小数
    输出(第k个最小号为:%d个,网络连接);    系统(暂停);
}

正如你可以看到10 ^ 10是一个非常大的价值,我做别的东西来填充的随机数的数组。这是对的吗?有没有别的东西,我可以做什么?
我的第二个问题是查找算法。它不工作。谁能帮我这些?非常感谢你。


解决方案

 长长get_big_rand()
{    长长的结果;
    做{
        结果=(RAND()及0x3FF处);
        结果<< = 12;
        结果| =(RAND()及0xFFF的);
        结果<< = 12;
        结果| =(RAND()及0xFFF的);
    }而(++结果> 10000000000ULL);
    返回结果;
}

a part of an assignment of mine is based on an array (its size is given by the user) which contains random numbers from 1 to 10^10. Then we have to find the k-th smaller number of the array. Here's what I tried:

#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <time.h>

using namespace std;

void swap(int *x,int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int choose_pivot(int i,int j )
{
    return((i+j) /2);
}

// Print array
void printarr(int arr[],int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d\t",arr[i]);
}

// Find algorithm
int find1(int arr[],int left,int right,int k)
{
    int i,j,pivot;
    if (left==right)
        return arr[left];
    else
    {
        i=left;
        j=right+1;
        pivot= arr[left];
        do
        {
            do {
                i=i+1;
            } while (arr[i]>=pivot);
            do {
                j =j-1;
            } while (arr[j]<=pivot);
            if (i<j)
                swap(arr[i],arr[j]);
        } while (j<=i);
    }
    swap(arr[left],arr[j]);
    if (k==j)
        return arr[j];
    else if (k<j)
        find1(arr,left,j-1,k);
    else 
        find1(arr,j+1,right,k-j);
}

int main(int argc, char *argv[])
{
    srand(time(NULL));
    int n,i,fi,k;
    printf("Give array's size:\n");
    scanf("%d",&n);
    int pin[n];
    for (i=0;i<n;i++)
        pin[i]=((rand()*rand()) % 1000000000) +1;
    printf("Give k: \n");
    scanf("%d",&k);
    printf("The array contains the following numbers:\n\n");
    printarr(pin,n);
    fi=find1(pin,0,n-1,k);//find the k-th smallest number in the array
    printf("The k-th smallest number is: %d",fi);

    system("PAUSE");
}

As you can see 10^10 is a very big value, and I did something else to fill the array with the random numbers. Is it correct? Is there something else I could do? And my second problem is on the find algorithm. It doesn't work. Could anyone help me with these? Thank you very much

解决方案

long long get_big_rand()
{

    long long result;
    do {
        result = (rand() & 0x3ff);
        result <<= 12;
        result |= (rand() & 0xfff);
        result <<= 12;
        result |= (rand() & 0xfff);
    } while (++result > 10000000000ULL);
    return result;
}

这篇关于填充随机数的数组从C或C 1到10 ^ 10 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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