关闭

二叉堆

标签: 二叉堆数据结构
664人阅读 评论(0) 收藏 举报
分类:
#include<stdio.h>
#include<stdlib.h>

int lowbit(int x)
{
	int z;
	z = ~x;
	z++;
	return (x & z);
}
//测试
/*int main(void)
{
	int n,t;
	n = 50;
	while(n)
	{
		t = lowbit(n);
		printf("%d\n",t);
		n -= t;
	}	
	return 0;
}*/
/*求和*/
int Sum(unsigned int *C,unsigned n)
{
	int sum  = 0;
	while(n > 0)
	{
		sum += C[n];
		//printf("%d ",sum);
		n -= lowbit(n);
	}
	return sum;
}
/*修改某位置的值*/
void Change(unsigned* c,int n,int a)
{
	int m;
	printf("当前位置现在为%d,亲输入你要赋予它的新值\n",a);
	scanf("%d",&m);
	m = -a+m;
	while(n <= 1000)
	{
		c[n] += m;
		n += lowbit(n);
	}
}
int main(void)
{
	unsigned int A[1000],c[1000];
	unsigned i,j,k,n,s,m;
	unsigned int N;
	scanf("%u",&N);
	///scanf("%u",A[1]);
	for(i = 1;i <= N;i ++)
	{
		k = 1;
		//m = i;
		scanf("%u",&A[i]);
		c[i] = A[i];
		if(j = lowbit(i))
		{
			while(k < j)
			{
				c[i] += A[i - k];
				k ++;
			}
		}
	}
	for(i = 1;i <= N;i ++)
		printf("%d ",c[i]);
	printf("请输入你想求前多少个数的和\n");
    scanf("%d",&n);
	s = Sum(c,n);
	printf("%d\n",s);
	printf("请输入你想修改的数的位置:\n");
	scanf("%d",&n);
	Change(c,n,A[n]);
	for(i = 1;i <= N;i ++)
		printf("%d ",c[i]);
	printf("请输入你想求前多少个数的和\n");
	printf("请输入你想求前多少个数的和\n");
    scanf("%d",&n);
	s = Sum(c,n);
	printf("%d\n",s);
	return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:20887次
    • 积分:512
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条