sicily 1214. 信号分析

//一道bt的题目,求解过程请看张大牛的ppt
//变量要声明为unsigned int, 一直错, Fail at test case #10 ,改了就AC了
#include<iostream>
#include
<cstring>
#include
<string>
#include
<math.h>
using namespace std;
int main()
{
string str;
unsigned
int l; //题目要求 1<=L<2^32, 如果 声明为 int l; 则会WA Fail at test case #10
int len,sum;
cin
>>l;
while(l)
{
str.insert(str.begin(),l
%2+48);
l
/=2;
}
len
=str.length();
if((len-1)%2==1)
sum
=2*pow(2.0,(len-2)/2.0)-2+pow(2.0,(len-2)/2.0);
else
sum
=2*pow(2.0,(len-1)/2.0)-2;
//sum表示长度小于len的所有回文字符串的数量和

char ch[100],temp[100];
temp[
1]=temp[len]='1';
for(int i=0;i<len;++i)
ch[i
+1]=str[i];

for(int i=2;i<=(len+1)/2;++i) //长度为len,小于输入str的回文字符串
{
if(ch[i]=='0')
temp[i]
=temp[len+1-i]='0';
else
{
int a=len+1-2*i+1;
sum
+=pow(2.0,a/2+a%2-1.0);
temp[i]
=temp[len+1-i]='1';
}
}
ch[
0]=temp[0]='0';ch[len+1]=temp[len+1]='\0'; //是为了实现下面的strcmp函数
if(strcmp(temp,ch)<=0)
sum
++;
cout
<<sum<<endl;
return 0;
}

posted on 2011-07-05 02:09  sysu_mjc  阅读(418)  评论(0编辑  收藏  举报

导航