提交时间:2024-08-20 22:12:25
运行 ID: 173617
#include<bits/stdc++.h> using namespace std; //n为操作数,r为递归深度 void fun(int n, int r) { //递归结束,最先输出,不带+号 if (1 == n) { /*cout<<"2("<<r<<")";*/ //将r表示成0和2 switch(r) { //2^0,递归深度为0 case 0:cout<<"2(0)";break; //2^1,递归深度为1 case 1:cout<<"2";break; //2^2,递归深度为2 case 2:cout<<"2(2)";break; //2^r,递归深度为r default: cout<<"2("; fun(r,0); cout<<")"; } } else { //n除以二,递归深度加1 fun(n/2, r+1); //如果模2有余数,则为2^r if (1 == n%2) { /*cout<<"+2("<<r<<")";*/ /*cout<<"2("<<r<<")";*/ //将r表示成0和2 switch(r) { //2^0,递归深度为0 case 0:cout<<"+2(0)";break; //2^1,递归深度为1 case 1:cout<<"+2";break; //2^2,递归深度为2 case 2:cout<<"+2(2)";break; //2^r,递归深度为r default: cout<<"+2("; fun(r,0); cout<<")"; } } } }