#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 ",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,亲输入你要赋予它的新值 ",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("请输入你想求前多少个数的和 "); scanf("%d",&n); s = Sum(c,n); printf("%d ",s); printf("请输入你想改动的数的位置: "); scanf("%d",&n); Change(c,n,A[n]); for(i = 1;i <= N;i ++) printf("%d ",c[i]); printf("请输入你想求前多少个数的和 "); printf("请输入你想求前多少个数的和 "); scanf("%d",&n); s = Sum(c,n); printf("%d ",s); return 0; }