C language Written test: top K question

Source: Internet
Author: User

After several C-language written examinations, it is often asked to ask for the first 100 of the 10,000 numbers, and now think of two ways to solve:

Method 1:1-dimensional values

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

To find the maximum value of the array, return B "0"
int max (int b[],int N)
{
int i,t,k=0,m=0;
int c=n;
The array 22 is compared and the larger value is
while (n!=1)
{
for (i=0;i<n;i++)
{
if (b[i]>b[i+1])
{
t=0;
T=b[i];
B[I]=B[I/2];
b[i/2]=t;
}
else{
t=0;
T=B[I+1];
B[I+1]=B[I/2];
b[i/2]=t;
}
i++;
}
N=N/2;
}
M=B[0];
return m;
}
int main (int argc, char *argv[])
{
int *test,i,k,d=0,x=0,y=0;
int l=100000,h=100; L represents the total number of numbers, h denotes the required number of first H
int m[h];
D=l;
if (d& (d-1))
{
while (d>1) {
d>>=1;
x + +;}
x + +;
}
Else
{
while (d>1) {
d>>=1;
x + +;} X indicates the number of comparisons to be made, and Y indicates the amount of space to be requested
}
Y=pow (2,X);
Srand (Time (0));
test= (int *) malloc (sizeof (int) *y);
for (long a=0;a<l;a++)
{
Test[a]=rand ()%100000+1; Generate random numbers dynamically
printf ("%d", Test[a]);
}
printf ("\ n");
for (i=l;i<y;i++)
{
test[i]=0;
}
M[0]=max (Test,y);
for (i=1;i{
for (k=0;k<l;k++)
{
if (Test[k]==m[i-1])
test[k]=0;
}
M[i]=max (Test,y);
}
printf ("The first%d of the%d numbers is: \ n", l,h);
for (i=0;i{
printf ("%d", m[i]);
}
printf ("\ n");
printf ("Single-time Comparison:%d\n", X);
printf ("Total number of comparisons:%d\n", h*x);
printf ("\ n");
Free (test);
Test=null;
return 0;
}

Method Two: Huffman tree

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int ELEMTYPE;
struct Btreenode
{
Elemtype data;
struct btreenode* left;
struct btreenode* right;
};
Establishes a Huffman tree based on n weights in array A, returning the root pointer
struct btreenode* Createhuffman (elemtype a[], int n)
{
int I, J;
struct Btreenode **b, *q;
b = (Btreenode *) malloc (sizeof (struct btreenode) *n);
for (i = 0; i < n; i++)//initialize B pointer array so that each pointer element points to the corresponding element node in the A array
{
B[i] = (Btreenode *) malloc (sizeof (struct btreenode));
B[i]->data = A[i];
B[i]->left = B[i]->right = NULL;
}
for (i = 1; i < n; i++)//n-1 cycles established Huffman tree
{
K1 represents the subscript of a tree root node with a minimum weight in the forest, K2 the sub-minimum subscript
int k1 =-1, K2;
for (j = 0; J < N; j + +)//Let K1 initially point to the first tree in the forest, K2 point to the second tree
{
if (b[j]! = NULL && K1 = =-1)
{
K1 = j;
Continue
}
if (b[j]! = NULL)
{
K2 = j;
Break
}
}
for (j = K2; J < N; j + +)//from the current forest to find the minimum weight tree and the second smallest
{
if (b[j]! = NULL)
{
if (B[j]->data < B[k1]->data)
{
K2 = K1;
K1 = j;
}
else if (B[j]->data < B[k2]->data)
K2 = j;
}
}
Create a new tree from the minimum weight tree and the sub-minimum weight tree, Q points to the root node
Q = (Btreenode *) malloc (sizeof (struct btreenode));
Q->data = b[k2]->data;
Q->left = B[k1];
Q->right = B[k2];

B[K1] = q;//assigns a pointer to the new tree to the K1 position in the B-pointer array
B[K2] = null;//k2 position is empty
}
Free (b); Delete the dynamically created array b
return q; Returns the root pointer of the entire Huffman tree
}
int main (int argc, char *argv[])
{
int *test;
int l=100,h=10,i,k;
int m[h];
struct btreenode* q;
Srand (Time (0));
test= (int *) malloc (sizeof (int) *l);
for (long a=0;a<l;a++)
{
Test[a]=rand ()%100+1;
printf ("%d", Test[a]);
}
printf ("\ n");
Q=createhuffman (test,l);
m[0]=q->data;
for (i=1;i{
for (k=0;k<l;k++)
{
if (Test[k]==m[i-1])
test[k]=0;
}
Q=createhuffman (test,l);
m[i]=q->data;
}
printf ("The first%d of the%d numbers is: \ n", l,h);
for (i=0;i{
printf ("%d", m[i]);
}
printf ("\ n");
printf ("Number of comparisons:%d", l-1);
printf ("\ n");
return 0;
}

The results of the operation are as follows:

C language Written test: top K question

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.