[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>2的幂次方表示
任何一个正整数都可以用2的幂次方表示。例如:
137=27+23+20
同时约定方次用括号来表示,即ab可表示为a(b)。由此可知,137可表示为:
2(7)+2(3)+2(0)
进一步:7=22+2+20(21用2表示)
3=2+20
所以最后137可表示为:
2(2(2)+2+2(0))+2(2+2(0))+2(0)
又如:
1315=210+28+25+2+1
所以1315最后可表示为:
2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
输入
一个正整数n(n≤20000)。
输出
一行,符合约定的n的0,2表示(在表示中不能有空格)。
样例输入
137
样例输出
2(2(2)+2+2(0))+2(2+2(0))+2(0)
个人思路: 递归
去找规律,以及边界条件。2的幂次方表示,离不开除以2,以及除了多少个2,所以函数convert(m,n),m表示现在正处理的数,n表示上一个处理的数可以用几个二次幂表示,相当于存储了上一个数。如果m为奇数,就需要将m分解为1+(m-1)。再对m-1(此时是偶数)和1分别处理。
当除尽且n大于三时,像2的7次方,7还需要分解,这时就需要写套娃代码了,将7再去处理,
if(m==1&&n>=3) { cout<<"2("; convert(n,0); cout<<")"; }
最后找边界,因为输出时只有2和0两个数,可知正整数都可以分解为1,2的和,即2的0次方,2的1次方,当然2的2次方需要单独列出来。
convert(1,2) 输出2(2)
convert(1,1)输出2
convert(1,0)输出2(0)
(欢迎小伙伴们在评论区留下自己的意见~)
心得:
第一次独立写了一道递归,终于觉得有所进步了!!o(╥﹏╥)o!!一开始没思路,去搜答案,但是大部分用的位运算,因为我没看懂,所以抽了两小时写了个菜鸡水平的递归。
最后祝各位元旦快乐,bug越写越少,头发浓密。= ~ =
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
void convert(int m,int n) { int i; int flag_odd=0;//判断当前处理数字的奇偶,因为奇数需要分出一个处理 if(
m%2==1) flag_odd=1; //m==1表示当前处理数字除尽了,必须打印了 if(m==1&&n==0) cout<<"2(0)";//2的0次方
else if(m==1&&n==1) cout<<"2";//2的1次方 else if(m==1&&n==2) cout<<"2(2)";//2的2次方
else if(m>1) //如果当前数未除尽 { if(flag_odd)//奇数 { convert(m/2,n+1); cout<<"+";
convert(1,n);//分数一个1处理,n不变 } else //偶数 convert(m/2,n+1); } else if(m==1&&n>=3)
//需要套娃的情况,次方还能分解 { cout<<"2("; convert(n,0); cout<<")"; } } int main() { int m;
cin>>m; convert(m,0); cout<<endl; }