gpt4 book ai didi

algorithm - 如何找到二进制表示为回文的数字总数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:47:27 25 4
gpt4 key购买 nike

找出二进制表示为回文的两个给定数字之间的数字总数的最佳方法是什么? The problem I am trying to solve is here on spoj http://www.spoj.com/problems/BINPALI/

最佳答案

我解决了spoj问题,代码如下:

#include<iostream>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<string>
using namespace std;
int main()
{
int a,b,t;
cin>>t;
while(t--)
{
cin>>a>>b;
int total=0;
string s="";
while(a<=b)
{
s="";
for(int i=a;i>0;i=i/2)
{
if(i%2)
s+='1';
else
s+='0';
}
string s2="",s3="";
s2=s.substr(0,s.length()/2);
int k=s.length();
if(k%2)
s3=s.substr(s.length()/2+1,s.length());
else
s3=s.substr(s.length()/2,s.length());
reverse(s2.begin(),s2.end());
if(s2==s3)
{
cout<<a<<" ";
total++;
}
a++;
}
if(!total)
cout<<"none"<<endl;
}
return 0;
}

关于algorithm - 如何找到二进制表示为回文的数字总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com