Chapter 1 of Programming

Source: Internet
Author: User

Question:A file that contains up to n positive integers, each of which is smallerN, WhereN = 10 ^ 7,All positive integers are unique. HowNIntegers are listed in ascending order.

Constraints:Maximum1 MBMemory space is available, with sufficient disk storage space.

 

Exercise 2 Exercise 3 implement bitvectors for sorting.

#include<stdio.h>#define N 10000000#define Shift 5#define BitPerWord (sizeof(int)*8)#define Mask ((1<<Shift)-1)int a[1+N/BitPerWord];void set(int i){    a[i>>Shift] |= 1<<(i&Mask);}void clr(int i){    a[i>>Shift] &= ~(1<<(i&Mask));}int test(int i){    return a[i>>Shift] & (1<<(i&Mask));}int main(){    freopen("data.in","r",stdin);    freopen("data.out","w",stdout);    for(int i=0;i<N;i++)        clr(i);    int a;    while(scanf("%d",&a)!=EOF)        set(a);    for(int i=0;i<N;i++){        if(test(i))            printf("%d ",i);    }    printf("\n");    return 0;}

 

Exercise 4: Return K random integers in the ascending order between 0 and n-1.

for i = [0,n)    x[i]=ifor i = [0,k)    swap(i,rand(i,n-1))    print(x[i])

 

Chapter 1 of Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.