C++ 蓝桥杯 求助 这道题该怎么做?
发布网友
发布时间:2023-06-24 19:30
我来回答
共2个回答
热心网友
时间:2024-07-12 11:50
//写出来啦,测试通过,如果有疑问,欢迎交流
//用递归写这个题,有个感悟,应该一步一步的实现,而且刚开始不要关注细节处理
//(对2的输出)
//第一步先把:137可表示为:2(7)+2(3)+2(0) 实现了(见最后注释掉的部分)
//第二步:只要修改count的地方就可以了
#include<iostream>
using namespace std;
void print_binary2(int tar, int count){
if(tar==0)
return;
if(tar%2!=0){
if(tar!=1){
print_binary2(tar /2, count+1);
cout<<'+';
}
if(count == 1){
cout<<2;
}else{
cout<<2;
cout<<'(';
if(count == 0)
cout<<count;////////////
else
print_binary2(count, 0);
cout<<')';
}
}else{
print_binary2(tar /2, count+1);
}
}
int main(){
int cur_max_count = 0;
print_binary2(1315, 0);
return 0;
}
//First step
//void print_binary2(int tar, int count){
//if(tar==0)
//return;
//if(tar%2!=0){
//if(tar!=1){
//print_binary2(tar /2, count+1);
//cout<<'+';
//}
//cout<<2;
//cout<<'(';
//cout<<count;////////////
//cout<<')';
//}else{
//print_binary2(tar /2, count+1);
//}
//}
热心网友
时间:2024-07-12 11:50
提示是扯淡的。这题压根用不到递归。n的范围是0~20000 (就算是到64位也用不到,吃饱了撑的递归)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string fix[]=
{
"2(0)",//0
"",//1次不用输出
"2",//2
"2+2(0)",//3
"2(2)",//4
"2(2)+2(0)",//5
"2(2)+2",//6
"2(2)+2+2(0)",//7
"2(2+2(0))",//8
"2(2+2(0))+2(0)",//9
"2(2+2(0))+2",//10
"2(2+2(0))+2+2(0)",//11
"2(2+2(0))+2(2)",//12
"2(2+2(0))+2(2)+2(0)",//13
"2(2+2(0))+2(2)+2",//14
"2(2+2(0))+2(2)+2+2(0)"//15
};
vector<int> fenjie(int a)
{
vector <int>s;
while (a)
{
if(a%2)
s.push_back(1);
else
s.push_back(0);
a>>=1;
}
return s;
}
int main(void)
{
vector <int>s;
int n;
cin>>n;
if(n==1)
{
cout<<"2(0)"<<endl;
return 0;
}
if(n==2)
{
cout<<"2"<<endl;
return 0;
}
if(n==3)
{
cout<<"2+2(0)"<<endl;
return 0;
}
s = fenjie(n);
int i;
string outstr="";
for (i=s.size()-1;i>1;i--)
if(s[i])
outstr+="2("+fix[i]+")+";
if (s[1])
outstr+="2+";
else
outstr = outstr.substr(0,outstr.size()-1);//删掉"+"
if(s[0])
outstr+=fix[0];
else if(s[1])
outstr = outstr.substr(0,outstr.size()-1);//删掉"+"
cout<<outstr<<endl;
return 0;
}
追问有错的地方 所测试的数据不能完全通过 蓝桥杯
追答
哦 由于比较急 尾部处理没做好 ,你再试试这个:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string fix[]=
{
"2(0)",//0
"",//1次不用输出
"2",//2
"2+2(0)",//3
"2(2)",//4
"2(2)+2(0)",//5
"2(2)+2",//6
"2(2)+2+2(0)",//7
"2(2+2(0))",//8
"2(2+2(0))+2(0)",//9
"2(2+2(0))+2",//10
"2(2+2(0))+2+2(0)",//11
"2(2+2(0))+2(2)",//12
"2(2+2(0))+2(2)+2(0)",//13
"2(2+2(0))+2(2)+2",//14
"2(2+2(0))+2(2)+2+2(0)"//15
};
vector<int> fenjie(int a)
{
vector <int>s;
while (a)
{
if(a%2)
s.push_back(1);
else
s.push_back(0);
a>>=1;
}
return s;
}
int main(void)
{
vector <int>s;
int n;
cin>>n;
if(n==1)
{
cout<<"2(0)"<<endl;
return 0;
}
if(n==2)
{
cout<<"2"<<endl;
return 0;
}
if(n==3)
{
cout<<"2+2(0)"<<endl;
return 0;
}
s = fenjie(n);
int i;
string outstr="";
for (i=s.size()-1;i>1;i--)
if(s[i])
outstr+="2("+fix[i]+")+";
if(!s[1]&&!s[0])//00
outstr = outstr.substr(0,outstr.size()-1);//删掉"+"
else if(!s[1]&&s[0])//01
outstr +="2(0)";
else if(s[1]&&!s[0])//10
outstr+="2";
else //11
outstr+="2+2(0)";
cout<<outstr<<endl;
return 0;
}