Programming Question-6

1.Two-Sum Problem

The goal of this problem is to implement a variant of the 2-SUM algorithm (covered in the Week 5 lecture on hash table applications)

The file contains 500,000 positive integers (there might be some repetitions!).This is your array of integers, with the  ith  row of the file specifying the  ith entry of the array.

Your task is to compute the number of target values  t  in the interval [2500,4000] (inclusive) such that there are distinct numbers  x,y  in the input file that satisfy  x+y=t . (NOTE: ensuring distinctness requires a one-line addition to the algorithm from lecture.)

Write your numeric answer (an integer between 0 and 1501) in the space provided.


#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
const char INFILE[] = "HashInt.txt";
#define MAX	10000000

void fetch_hash(long *hash)	{
  FILE *fp = fopen(INFILE,"r");
  char line[10];
  long i;
  for (i=0; i<MAX; i++)
	hash[i] = 0;
  while (fgets(line,10,fp))
	hash[atoi(line)] ++;
}
short two_sum(long *hash, long t)	{
  long x;
  for (x = 1; x < (t-1)/2; x++)	
	if ((hash[x])&&(hash[t-x]))
	  return 1;
  return 0;
}
 
void main()
{
  long *hash = calloc(MAX,sizeof(long));
  fetch_hash(hash);
  short i,count = 0;
  for (i = 2500; i <= 4000; i++)
	if (two_sum(hash,i))
	  count++;
  printf("count = %d\n",count);
}

2.Median Problem

The goal of this problem is to implement the "Median Maintenance" algorithm (covered in the Week 5 lecture on heap applications). The text file contains a list of the integers from 1 to 10000 in unsorted order; you should treat this as a stream of numbers, arriving one by one. Letting  xi  denote the  i th number of the file, the  k th median  mk  is defined as the median of the numbers  x1,,xk . (So, if  k  is odd, then  mk  is  ((k+1)/2) th smallest number among  x1,,xk ; if  k  is even, then  mk  is the  (k/2) th smallest number among  x1,,xk .)

In the box below you should type the sum of these 10000 medians, modulo 10000 (i.e., only the last 4 digits). That is, you should compute  (m1+m2+m3++m10000)mod10000 .

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include"heap.h"
const char INFILE[] = "Median.txt";
#define MAX	10000

short RETURN_MEDIAN(short *A1,short *A2,short x)	{
  if (x<A2[1])
	MAX_HEAP_INSERT(A1,x);
  else
	MIN_HEAP_INSERT(A2,x);
  if (A1[0]-A2[0]>1)	
	MIN_HEAP_INSERT(A2,EXTRACT_MAX(A1));
  if (A2[0]>A1[0])	
	MAX_HEAP_INSERT(A1,EXTRACT_MIN(A2));
  return A1[1];
}
void print(short *A)	{
  short i;
  for (i=1; i<=A[0]; i++)
	printf("%d ",A[i]);
}

void main()	{
  short *A1,*A2,m=0;
  A1 = calloc(1+MAX,sizeof(short));
  A2 = calloc(1+MAX,sizeof(short));
  A1[0] = 1; A1[1] = 0;
  A2[0] = 1; A2[1] = 20000;
  char line[10];
  FILE *fp = fopen(INFILE,"r");
  while (fgets(line,10,fp))	
	m = (m + RETURN_MEDIAN(A1,A2,atoi(line))) % 10000;
  printf("%d\n",m);
}

where "heap.h" contains many heap functions.

#ifndef _HEAP_H_
#define _HEAP_H_
//	MAX_HEAP
void MAX_HEAPIFY(short *A,short i)	{
	short left=2*i,right=2*i+1,larger,tmp;
	if ((left<=A[0])&&(A[left]>A[i]))
	  larger = left;
	else
	  larger = i;
	if ((right<=A[0])&&(A[right]>A[larger]))
	  larger = right;
	if (larger != i)	{
	  tmp = A[larger]; A[larger] = A[i]; A[i] = tmp;
	  MAX_HEAPIFY(A,larger);
	}
}
short EXTRACT_MAX(short *A)	{
  short max = A[1];
  A[1] = A[A[0]--];
  MAX_HEAPIFY(A,1);
  return max;
}
void HEAP_INCREASE_KEY(short *A,short i,short key)	{
  short tmp;
  A[i] = key;
  while ((i>1)&&(A[i/2]<A[i]))	{
	tmp = A[i]; A[i] = A[i/2]; A[i/2] = tmp;
	i = i/2;
  }
}
void MAX_HEAP_INSERT(short *A,short key)	{
  A[0]++;
  HEAP_INCREASE_KEY(A,A[0],key);
}
//	MIN_HEAP
void MIN_HEAPIFY(short *A,short i)	{
	short left=2*i,right=2*i+1,smaller,tmp;
	if ((left<=A[0])&&(A[left]<A[i]))
	  smaller = left;
	else
	  smaller = i;
	if ((right<=A[0])&&(A[right]<A[smaller]))
	  smaller = right;
	if (smaller != i)	{
	  tmp = A[smaller]; A[smaller] = A[i]; A[i] = tmp;
	  MIN_HEAPIFY(A,smaller);
	}
}
short EXTRACT_MIN(short *A)	{
  short min = A[1];
  A[1] = A[A[0]--];
  MIN_HEAPIFY(A,1);
  return min;
}
void HEAP_DECREASE_KEY(short *A,short i,short key)	{
  short tmp;
  A[i] = key;
  while ((i>1)&&(A[i/2]>A[i]))	{
	tmp = A[i]; A[i] = A[i/2]; A[i/2] = tmp;
	i = i/2;
  }
}
void MIN_HEAP_INSERT(short *A,short key)	{
  A[0]++;
  HEAP_DECREASE_KEY(A,A[0],key);
}
#endif


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值