#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;
}
二叉堆
最新推荐文章于 2024-08-14 21:15:33 发布