![]() |
#2
tornador2010-10-09 13:40
|

// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;
void BuildTreeArray(int* &a,int* &c,int len);
int LowBit(int x);
void Show(const int* a,int len);
int main()
{
int* a;
int* c;
int len;
cin>>len;
a=new int[len+1];
c=new int[len+1];
c[0]=0;
for(int i=0;i<=len;i++)
{
a[i]=i;
}
BuildTreeArray(a,c,len);
Show(c,len);
return 0;
}
void BuildTreeArray(int* &a,int* &c,int len)
{
c[1]=a[1];
for(int k=2;k<=len;k++)
{
c[k]=0;
for(int i=k;i>k-LowBit(k);i--)
{
c[k]+=a[i];
}
}
}
int LowBit(int x)
{
int i=1;
while(x&1)
{
x>>1;
cout<<"*****"<<endl;
i++;
}
return i*2;
//return x&(-x);
}
void Show(const int* a,int len)
{
for(int i=1;i<=len;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
这是我自己建的树状数组,其中LowBit()函数是用的我自己的方法,但编译程序时会有一条警告,warning:'>>'#include <iostream>
using namespace std;
void BuildTreeArray(int* &a,int* &c,int len);
int LowBit(int x);
void Show(const int* a,int len);
int main()
{
int* a;
int* c;
int len;
cin>>len;
a=new int[len+1];
c=new int[len+1];
c[0]=0;
for(int i=0;i<=len;i++)
{
a[i]=i;
}
BuildTreeArray(a,c,len);
Show(c,len);
return 0;
}
void BuildTreeArray(int* &a,int* &c,int len)
{
c[1]=a[1];
for(int k=2;k<=len;k++)
{
c[k]=0;
for(int i=k;i>k-LowBit(k);i--)
{
c[k]+=a[i];
}
}
}
int LowBit(int x)
{
int i=1;
while(x&1)
{
x>>1;
cout<<"*****"<<endl;
i++;
}
return i*2;
//return x&(-x);
}
void Show(const int* a,int len)
{
for(int i=1;i<=len;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
operator has no effect:expected operator has side-effect.运行时就出现死循环了..请大家看一下..谢了